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
Give me a +1 if you think it was a good article. Thanks!