This is a migrated thread and some comments may be shown as answers.

How to highlight and select Drop Target TreeViewItem?

5 Answers 237 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Sven
Top achievements
Rank 1
Sven asked on 23 Jul 2012, 06:14 PM
Hell out there!
Maybe sbd. out the can help me. I want to implement a drag and drop operation from RadGridView to RadTreeView. But I am too silly.
So, in GridView DragQuery event, I fill the payload:
private void grdSessions_DragQuery(object sender, Telerik.Windows.Controls.DragDrop.DragDropQueryEventArgs e)
{
    e.QueryResult = true;
    if (e.Options.Status != DragStatus.DragQuery)
        return;           
    var sessionToDrag = (XTSession)grdSessions.SelectedItem;
    if (sessionToDrag != null)
    {
        e.Options.Payload = sessionToDrag;
        e.Options.DragCue = RadDragAndDropManager.GenerateVisualCue();
        e.Options.ArrowCue = RadDragAndDropManager.GenerateArrowCue();               
    }           
}


I also implemented the DropQuery and DropInfo of the TreeView:

private void tvwNodes_DropQuery(object sender, Telerik.Windows.Controls.DragDrop.DragDropQueryEventArgs e)
        {
            if (e.Options.Payload is XTSession)
            {
                e.QueryResult = true;
            }           
        }
 
private void tvwNodes_DropInfo(object sender, DragDropEventArgs e)
        {           
            if (e.Options.Status == DragStatus.DropComplete)
            {
                if (e.Options.Payload is XTSession)
                {
                        // Do the stuff
                }
            }
        }


My Problem is now, that I have no Idea how to higlight and track the target treeviewitem? When I move the mouse cursor over a treenode in drop mode, I want this treenode to be highlighted. How can this be done? And how can I find out on which node the the drop was done in DropInfo event? The destination object contains the whole treeview. Also, I should disable drop if the mouse cursor is not over a treeviewitem. How can this be done?
Can anybody help me out?

Thanks in advance.

brgds
Sven Weiberg

5 Answers, 1 is accepted

Sort by
0
Accepted
Tina Stancheva
Telerik team
answered on 26 Jul 2012, 02:34 PM
Hello Sven,

First of all I'd advice you to take advantage of the new DragAndDropManager as it is more powerful and support a great number of drag and drop scenarios. This makes it a better choice when starting to build a drag/drop application. 

However, it is also important to note that the RadTreeView control built-in drag/drop implementation still relies on the old RadDragAndDropManager but we're planning to migrate it to the new manager in the future. 

Having that in mind, I prepared a sample solution demonstrating how to implement drag and drop operation between the RadTreeView and the RadGridView controls. The solution relies on the new DragAndDropManager and specifically on DragDropBehavior  classes that define the drag/drop logic behind both controls. 

Also, in order to add a visual feedback when dragging over a RadTreeViewItem, I added event handlers for the DragEnter and DragLeave events and I update the visual state of the tree items accordingly. Please have a look at this solution and let us know if it works for you or if we can assist you with anything else.

All the best,
Tina Stancheva
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Sven
Top achievements
Rank 1
answered on 26 Jul 2012, 05:12 PM
Ok, this gives me a little help. Thanks. But it seems to be much code, espacially for my needs. Isn't there a way to use the provided DragDropBehavior and derive from it?

brgds
Sven
0
Tina Stancheva
Telerik team
answered on 30 Jul 2012, 11:45 AM
Hello Sven,

At the moment this is the best approach for your scenario. However, once the RadTreeView built-in drag/drop logic is changed to use the new DragDropManager, there should be an easier implementation of your scenario. Unfortunately at this time I cannot bind to a timeframe when this task will be addressed.

Regards,

Tina Stancheva
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Sven
Top achievements
Rank 1
answered on 02 Aug 2012, 04:09 PM
Well. Unfortunately it does not work in my project. The DragInitialize Event is not called, allthough DragDropManager.AddDragInitializeHandler is executed at the beginning.
Do you have any Idea what I might have done wrong? I have no Idea...

brgds
Sven

private RadGridView _associatedObject;
        /// <summary>
        /// AssociatedObject Property
        /// </summary>
        public RadGridView AssociatedObject
        {
            get
            {
                return _associatedObject;
            }
            set
            {
                _associatedObject = value;
            }
        }
 
        private static Dictionary<RadGridView, SessionGridDragDropBehavior> instances;
 
        static SessionGridDragDropBehavior()
        {
            instances = new Dictionary<RadGridView, SessionGridDragDropBehavior>();
        }
 
        public static bool GetIsEnabled(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsEnabledProperty);
        }
 
        public static void SetIsEnabled(DependencyObject obj, bool value)
        {
            SessionGridDragDropBehavior behavior = GetAttachedBehavior(obj as RadGridView);
 
            behavior.AssociatedObject = obj as RadGridView;
 
            if (value)
            {
                behavior.Initialize();
            }
            else
            {
                behavior.CleanUp();
            }
            obj.SetValue(IsEnabledProperty, value);
        }
 
        // Using a DependencyProperty as the backing store for IsEnabled.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsEnabledProperty =
            DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(SessionGridDragDropBehavior),
                new PropertyMetadata(new PropertyChangedCallback(OnIsEnabledPropertyChanged)));
 
        public static void OnIsEnabledPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            SetIsEnabled(dependencyObject, (bool)e.NewValue);
        }
 
        private static SessionGridDragDropBehavior GetAttachedBehavior(RadGridView gridview)
        {
            if (!instances.ContainsKey(gridview))
            {
                instances[gridview] = new SessionGridDragDropBehavior();
                instances[gridview].AssociatedObject = gridview;
            }
 
            return instances[gridview];
        }
 
        protected virtual void Initialize()
        {
            this.UnsubscribeFromDragDropEvents();
            this.SubscribeToDragDropEvents();
        }
 
        protected virtual void CleanUp()
        {
            this.UnsubscribeFromDragDropEvents();
        }
 
        private void SubscribeToDragDropEvents()
        {
            DragDropManager.AddDragInitializeHandler(this.AssociatedObject, OnDragInitialize);
            DragDropManager.AddGiveFeedbackHandler(this.AssociatedObject, OnGiveFeedback);
            DragDropManager.AddDropHandler(this.AssociatedObject, OnDrop);
            DragDropManager.AddDragDropCompletedHandler(this.AssociatedObject, OnDragDropCompleted);
            DragDropManager.AddDragOverHandler(this.AssociatedObject, OnDragOver);
        }
 
        private void UnsubscribeFromDragDropEvents()
        {
            DragDropManager.RemoveDragInitializeHandler(this.AssociatedObject, OnDragInitialize);
            DragDropManager.RemoveGiveFeedbackHandler(this.AssociatedObject, OnGiveFeedback);
            DragDropManager.RemoveDropHandler(this.AssociatedObject, OnDrop);
            DragDropManager.RemoveDragDropCompletedHandler(this.AssociatedObject, OnDragDropCompleted);
            DragDropManager.RemoveDragOverHandler(this.AssociatedObject, OnDragOver);
        }
 
        private void OnDragInitialize(object sender, DragInitializeEventArgs e)
        {
            var data = (sender as RadGridView).SelectedItem;
        }
 
        private void OnGiveFeedback(object sender, Telerik.Windows.DragDrop.GiveFeedbackEventArgs e)
        {
            e.SetCursor(Cursors.Arrow);
            e.Handled = true;
        }
 
        private void OnDragDropCompleted(object sender, DragDropCompletedEventArgs e)
        {
        }
 
        private void OnDrop(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
        {
        }
 
        private void OnDragOver(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
        {
        }
0
Sven
Top achievements
Rank 1
answered on 02 Aug 2012, 04:47 PM
Ok. I found the solution. I did not set the attached dependcy property AllowDrag for the grid row.
Tags
TreeView
Asked by
Sven
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Sven
Top achievements
Rank 1
Share this question
or