DragDrop from ListBox to TreeView

2 posts, 0 answers
  1. Kyle
    Kyle avatar
    1 posts
    Member since:
    Oct 2011

    Posted 27 Oct 2011 Link to this post

    I have the following XAML defining elements on a control I'm using to learn more about drag/drop operations between controls:

    <telerik:RadTreeView x:Name="dictionaryTreeView" IsDragDropEnabled="True"
                         ItemTemplate="{StaticResource dictionaryItemTemplate}" ItemsSource="{Binding Items, Mode=TwoWay}"
                         Grid.Column="0" telerik:RadDragAndDropManager.AllowDrop="True">
         
    </telerik:RadTreeView>
    <ListBox x:Name="definitionListBox" ItemsSource="{Binding UnusedDefinitions, Mode=TwoWay}"
             Grid.Column="1" telerik:RadDragAndDropManager.AllowDrop="True" />


    I have successfully handled the drag from TreeView and Drop onto ListBox, but when I click on an item in the ListBox and try to drag it to the TreeView none of the query events fire and no drag/drop operation occurs.  Right now the "tree" event handlers have no code because I'm just setting breakpoints to see if the event fires.  Anyone see some obvious I've missed in my setup?

    Here's the code-behind:

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Windows.Controls;
    using Telerik.Windows.Controls;
    using Telerik.Windows.Controls.DragDrop;
    using ItemsControl = System.Windows.Controls.ItemsControl;
     
    namespace TreeToListSample
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
     
                RadDragAndDropManager.AddDragQueryHandler(definitionListBox, OnListBoxDragQuery);
                RadDragAndDropManager.AddDragInfoHandler(definitionListBox, OnListBoxDragInfo);
                RadDragAndDropManager.AddDropQueryHandler(definitionListBox, OnListBoxDropQuery);
                RadDragAndDropManager.AddDropInfoHandler(definitionListBox, OnListBoxDropInfo);
     
                RadDragAndDropManager.AddDragQueryHandler(dictionaryTreeView, OnTreeViewDragQuery);
                RadDragAndDropManager.AddDragInfoHandler(dictionaryTreeView, OnTreeViewDragInfo);
                RadDragAndDropManager.AddDropQueryHandler(dictionaryTreeView, OnTreeViewDropQuery);
                RadDragAndDropManager.AddDropInfoHandler(dictionaryTreeView, OnTreeViewDropInfo);
     
                var vm = new DictionaryModel
                             {
                                 Items = new ObservableCollection<DictionaryItem>
                                             {
                                                 new DictionaryItem
                                                     {
                                                         Word = "Word 1",
                                                         Definitions = new ObservableCollection<string>()
                                                     },
                                                 new DictionaryItem
                                                     {
                                                         Word = "Word 2",
                                                         Definitions = new ObservableCollection<string>
                                                                           {
                                                                               "A definition", "Another definition"
                                                                           }
                                                     }
                                             },
                                    UnusedDefinitions = new ObservableCollection<string>{"Definition 1", "Definition 2", "Definition 3"}
                             };
     
                DataContext = vm;
     
            }
     
            private void OnTreeViewDropInfo(object sender, DragDropEventArgs e)
            {
                 
            }
     
            private void OnTreeViewDropQuery(object sender, DragDropQueryEventArgs e)
            {
                 
            }
     
            private void OnTreeViewDragInfo(object sender, DragDropEventArgs e)
            {
                 
            }
     
            private void OnTreeViewDragQuery(object sender, DragDropQueryEventArgs e)
            {
                 
            }
     
            private void OnListBoxDropInfo(object sender, DragDropEventArgs e)
            {
                if (e.Options.Status == DragStatus.DropComplete)
                {
                    ItemsControl target = e.Options.Destination as ItemsControl;
                    ItemsControl source = e.Options.Source as RadTreeViewItem;
     
                    if (target == null || e.Options.Payload == null || source == null) return;
     
                    var payload = e.Options.Payload as IEnumerable<object> ?? Enumerable.Empty<object>();
                    var targetCollection = target.ItemsSource as ObservableCollection<string>;
     
     
                    foreach (var item in payload)
                    {
                        targetCollection.Add(item.ToString());
                    }
                    e.Handled = true;
                }
            }
     
            private void OnListBoxDropQuery(object sender, DragDropQueryEventArgs e)
            {
                if(e.Options.Status == DragStatus.DropDestinationQuery)
                {
                    e.QueryResult = true;
                    e.Handled = true;
                }
                else if ( e.Options.Status == DragStatus.DropSourceQuery)
                {
                    e.QueryResult = true;
                    e.Handled = true;
                }
            }
     
            private void OnListBoxDragInfo(object sender, DragDropEventArgs e)
            {
                if(e.Options.Status == DragStatus.DragInProgress)
                {
                    e.Options.ArrowCue = RadDragAndDropManager.GenerateArrowCue();
                }
            }
     
            private void OnListBoxDragQuery(object sender, DragDropQueryEventArgs e)
            {
                if (e.Options.Status == DragStatus.DragQuery)
                {
                    e.Options.Payload = definitionListBox.SelectedItem;
                    e.Options.ArrowCue = RadDragAndDropManager.GenerateArrowCue();
                    e.QueryResult = true;
                    e.Handled = true;
                }
            }
        }
     
        public class DictionaryModel
        {
            public ObservableCollection<DictionaryItem> Items { get; set; }
            public ObservableCollection<String> UnusedDefinitions { get; set; }
        }
     
        public class DictionaryItem
        {
            public string Word { get; set; }
            public ObservableCollection<string> Definitions { get; set; }
        }
    }

  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 01 Nov 2011 Link to this post

    Hello Kyle,

    The reason that you cannot drag from the ListBox is that you allowed only dropping to it (by setting only AllowDrop property to "true"). 
    I would recommend you to take a look at our online documentation and demos for more information.
    Furthermore, I am attaching a sample project that you can use for additional reference.
     

    All the best,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
Back to Top