view Chronosv2/source/DragAndDrop/TreeviewDragDropDataProvider.cs @ 19:09d18d6e5f40

airport work
author stevenh7776 stevenhollidge@hotmail.com
date Thu, 15 Mar 2012 06:59:15 +0000
parents 443821e55f06
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
    }
}