view Chronosv2/source/DragAndDrop/TreeviewDragDropDataProvider.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children
line wrap: on
line source

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Xml;
using Chronos.Extensions.Windows;

namespace Chronos.Presentation.DragAndDrop
{
    public sealed class TreeViewDragDropDataProvider : IDataDropObjectProvider
    {
        #region · Fields ·

        private TreeView treeview;

        #endregion

        #region · Properties ·

        public DragDropProviderActions SupportedActions
        {
            get
            {
                return DragDropProviderActions.Data |
                       DragDropProviderActions.MultiFormatData;
            }
        }

        #endregion

        #region · Constructors ·

        public TreeViewDragDropDataProvider(TreeView treeview)
        {
            this.treeview = treeview;
        }

        #endregion

        #region · IDataDropObjectProvider Members ·

        public void AppendData(ref IDataObject data, MouseEventArgs e)
        {
            if (!(this.treeview.InputHitTest(e.GetPosition(e.OriginalSource as UIElement)) is TreeView) 
                && !(this.treeview.InputHitTest(e.GetPosition(e.OriginalSource as UIElement)) is ScrollViewer)
                && !(e.OriginalSource is Thumb))
            {
                TreeViewItem selectedUIelement = this.GetVisual(e).GetParent<TreeViewItem>();

                if (selectedUIelement != null && selectedUIelement.Items.Count == 0)
                {
                    object selectedItem = this.treeview.SelectedItem;

                    if (selectedItem != null)
                    {
                        if (selectedItem.GetType() == typeof(XmlElement))
                        {
                            data.SetData(DataFormats.Text, ((XmlElement)selectedItem).OuterXml);
                        }
                        else
                        {
                            data.SetData(DataFormats.Text, selectedItem.ToString());
                        }

                        data.SetData(selectedItem.GetType().ToString(), selectedItem);
                    }
                    else
                    {
                        data = null;
                    }
                }
                else
                {
                    data = null;
                }
            }
            else
            {
                data = null;
            }
        }

        public object GetData()
        {
            return this.treeview.SelectedItem;
        }

        public UIElement GetVisual(MouseEventArgs e)
        {
            // return this.treeview.ItemContainerGenerator.ContainerFromItem(this.treeview.SelectedItem) as UIElement;
            return e.OriginalSource as UIElement;
        }

        public void GiveFeedback(System.Windows.GiveFeedbackEventArgs args)
        {
            throw new NotImplementedException("Forgot to check the Supported actions??");
        }

        public void ContinueDrag(System.Windows.QueryContinueDragEventArgs args)
        {
            throw new NotImplementedException("Forgot to check the Supported actions??");
        }

        public bool UnParent()
        {
            // We are passing data, nothing to unparent 
            throw new NotImplementedException("We are passing data, nothing to unparent... what up ");
        }

        #endregion
    }
}