I have seen plenty of questions around drag-drop operations within a TreeView and I thought of dedicating a post to it. Now usually when one thinks of implementing a Drag Drop operation on a TreeView, there are a couple of events like the DragEnter, DragLeave, DragOver and Drop that are to be raised and handled. In some cases, you must also cancel the drag-n-drop operation if the drop is invalid. Overall, this looks like a lot of work for a developer who wants to quickly implement drag-n-drop functionality on his/her controls.
With the addition of the drag-drop targets for controls like the TreeView (and some others like the ListBox, DataGrid and DataPointSeries), doing a drag and drop operation in a TreeView is a cakewalk. Here’s a simple example:
Note: Make sure you have added a reference to the System.Windows.Toolkit. You can download the latest Silverlight toolkit over here
<UserControl
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
x:Class="SilverlightDragDropTreeView.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="250" d:DesignWidth="300">
<Canvas x:Name="LayoutRoot">
<toolkit:TreeViewDragDropTarget AllowDrop="True" HorizontalContentAlignment="Stretch">
<sdk:TreeView Height="250" >
<sdk:TreeViewItem Header="Permanent">
<sdk:TreeViewItem Header="Jill"/>
<sdk:TreeViewItem Header="Yahana"/>
<sdk:TreeViewItem Header="Kapil"/>
</sdk:TreeViewItem>
<sdk:TreeViewItem Header="Temporary">
<sdk:TreeViewItem Header="Matthew"/>
<sdk:TreeViewItem Header="Sarak"/>
<sdk:TreeViewItem Header="Jack"/>
</sdk:TreeViewItem>
</sdk:TreeView>
</toolkit:TreeViewDragDropTarget>
</Canvas>
</UserControl>
As you can see, the TreeView control is wrapped inside a TreeViewDragDropTarget which has the UIElement.AllowDrop property set to True. The UIElement.AllowDrop property determines whether this UIElement can be a drop target for purposes of Silverlight drag-and-drop operations. Setting this property to true enables it to raise or handle events like DragEnter, DragLeave, DragOver and Drop.
That is all that is required to enable Drag-Drop operations on a TreeView. Run the code and you will see that you can drop items between the Permanent and Temporary Categories
After dragging and dropping ‘yahana’ from Permanent to Temporary
Binding the TreeView with a Parent-Child Collection Object
As you can observe, to keep this article simple and to the point, I have hardcoded the TreeViewItem’s in the markup. In a real world application, you will rarely do that. Instead you would want to bind a custom collection of objects to the TreeView.
Let’s walkthrough the logical steps to bind the TreeView to a collection of objects. This example assumes that there are two collections:
- EmployeeCategory collection containing a EmpCategoryName property with two values, Permanent and Temporary
- Employee collection within each EmployeeCategory containing EmployeeName field
Here’s some ‘psuedocode’ of what the EmployeeCategory class would look like
public class EmployeeCategory implements INotifyPropertyChanged
{
public string EmpCategoryName { get; set; } // onPropertyChanged
public List<Employee> _employees { get; set; } // onPropertyChanged
}
So each parent level of the TreeView will be bound to the EmpCategoryName property from the EmployeeCategory, whereas the second level will display the EmployeeName in each EmployeeCategory collection.
To bind this collection to the TreeView, we will use a TreeView.ItemTemplate. Since we are using a Parent-Child relationship here, we will use two HierarchicalDataTemplate templates. Here’s how the markup would look like
<toolkit:TreeViewDragDropTarget AllowDrop="True" VerticalContentAlignment="Stretch">
<toolkit:TreeViewDragDropTarget.Resources>
<sdk:HierarchicalDataTemplate x:Key="EmpCategory"
ItemsSource="{Binding Employee}" ItemTemplate="{StaticResource EmpNames}">
<TextBlock Text="{Binding EmpCategoryName}"/>
</sdk:HierarchicalDataTemplate>
<sdk:HierarchicalDataTemplate x:Key="EmpNames"
<TextBlock Text="{Binding EmployeeName}"/>
</sdk:HierarchicalDataTemplate>
</toolkit:TreeViewDragDropTarget.Resources>
<sdk:TreeView x:Name="tView" ItemTemplate="{StaticResource EmpCategory}"
Height="250" >
</sdk:TreeView>
</toolkit:TreeViewDragDropTarget>
The data can be pulled using a WCF service which will query the EmployeeCategory and Employee classes and bind it to the TreeView. I hope these steps will guide you how to bind the TreeView to a custom collection object. Let me know if you need a complete article on this with the WCF service implementation and I will write one for you.
I hope this article was useful and I thank you for viewing it. The entire source code of this article can be downloaded over here
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn