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

TreeViewDragDropOptions is always null

2 Answers 145 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Sylvain
Top achievements
Rank 1
Sylvain asked on 17 Feb 2017, 02:31 PM

Hello Telerik,

During a Drag&Drop in a RadTreeView, I encounter some troubles with TreeViewDragDropOptions where the dropped item is positioned. Ideally I would like to get the objects that are positioned before and after (if they exist) the dropped item.

However, following multiple found example (like this one), in my case, the content of TreeViewDragDropOptions is always null.

Those are the objects I am binding to the TreeView:

Node.cs

public class Node<T>
{
   public T Source
   {
      get; set;
   }
 
   public Node<T> Parent
   {
      get; set;
   }
 
   public List<Node<T>> Children = new List<Node<T>>();
}

 

Here T is a Category

Category.cs

public class Category
{
   public string Title
   {
      get; set;
   }
 
   public int Sort
   {
      get; set;
   }
}

 

In the ViewModel, an ObservableCollection contains the hierarchy.

public ObservableCollection<Node<Category>> MyCategories
{
  [...]
}

 

Finally, the Views:

MyView.xaml

<telerik:RadTreeListView Name="CategoriesRadTreeView"
  IsDragDropEnabled="True" telerik:TreeViewSettings.DragDropExecutionMode="New"
  ItemsSource="{Binding MyCategories}"
  AutoGenerateColumns="False">
 
   <telerik:RadTreeListView.ChildTableDefinitions>
      <telerik:TreeListViewTableDefinition ItemsSource="{Binding Children}" />
   </telerik:RadTreeListView.ChildTableDefinitions>
          
   <telerik:RadTreeListView.SortDescriptors>
      <telerik:SortDescriptor Member="Source.Sort" SortDirection="Ascending" />
   </telerik:RadTreeListView.SortDescriptors>
          
   <telerik:RadTreeListView.Columns>
      <telerik:GridViewDataColumn DataMemberBinding="{Binding Source.Title}" Header="Title" />
      <telerik:GridViewDataColumn DataMemberBinding="{Binding Source.Sort}" Header="Sort" />
   </telerik:RadTreeListView.Columns>
</telerik:RadTreeListView>

 

And the code behind:

MyView.xaml.cs

public WebstoreKatalogView()
{
   InitializeComponent();
 
   DragDropManager.AddDragInitializeHandler(CategoriesRadTreeView, OnDragInitialize);
   DragDropManager.AddDragOverHandler(CategoriesRadTreeView, OnDragOver, true);
   DragDropManager.AddDropHandler(CategoriesRadTreeView, OnDrop);
}
 
private void OnDragInitialize(object sender, DragInitializeEventArgs e)
{
   e.AllowedEffects = DragDropEffects.All;
   var payload = DragDropPayloadManager.GeneratePayload(null);
   var data = ((FrameworkElement)e.OriginalSource).DataContext;
   payload.SetData("DragData", data);
   e.Data = payload;
   e.Handled = true;
}
 
private void OnDragOver(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
{
   var options = DragDropPayloadManager.GetDataFromObject(e.Data, TreeViewDragDropOptions.Key) as TreeViewDragDropOptions;
   if (options != null)
   {
      // never reaches here!
   }
}
 
private void OnDrop(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
{
   // this works!
   var node = DragDropPayloadManager.GetDataFromObject(e.Data, "DragData");
}

 

I have no idea what is wrong or what could be missing.

Thank you very much for your help!

 

Sylvain

 

2 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 22 Feb 2017, 07:31 AM
Hi Sylvain,

After reviewing the provided code, I noticed that the RadTreeListView component is defined, but a drag and drop implementation for the RadTreeView is used. Note, that these are two completely separate controls. If you plan to use RadTreeListView, you can take a look at the Drag and Drop WPF Demo. RadTreeView also has a WPF example with the same name. You can also use the approach demonstrated in the SDK Example to which you referred, as it is for RadTreeView as well.

Hopefully, this clarifies your concerns.

Regards,
Stefan X1
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Sylvain
Top achievements
Rank 1
answered on 24 Feb 2017, 07:20 AM

Hi Stefan,

Thank you very much for your answer.

Indeed, I believe I got confused with the similar names of the controls.

After looking at the demo, I also moved my code in a behavior class and now everything is working perfectly.

Cheers,

 

Sylvain

Tags
TreeView
Asked by
Sylvain
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Sylvain
Top achievements
Rank 1
Share this question
or