New to Telerik UI for WPF? Download free 30-day trial

Enable Only Drop Inside

This tutorial demonstrates how to enable dropping inside the RadTreeViewItems, but not around them.

For the purpose of this tutorial will be used the following treeview declaration:

    <sampleData:RadTreeViewSampleData x:Key="DataSource"/> 
    <DataTemplate x:Key="Team"> 
        <TextBlock Text="{Binding Name}"/> 
    <HierarchicalDataTemplate x:Key="Division" 
        ItemsSource="{Binding Teams}" 
        ItemTemplate="{StaticResource Team}"> 
        <TextBlock Text="{Binding Name}"/> 
    <HierarchicalDataTemplate x:Key="League"  
        ItemsSource="{Binding Divisions}" 
        ItemTemplate="{StaticResource Division}"> 
        <TextBlock Text="{Binding Name}"/> 
<Grid x:Name="LayoutRoot" Background="White"> 
    <telerik:RadTreeView x:Name="radTreeView" IsDragDropEnabled="True" Margin="8" 
        ItemsSource="{Binding Source={StaticResource DataSource}, Path=LeaguesDataSource}" 
        ItemTemplate="{StaticResource League}"/> 

The RadTreeView is data bound to a collection of business objects. For more information, read the Binding to Object topic.

In order to implement only drop inside you need to perform the following steps:

  • Attach to the DragDropManager DragOver event:

        DragDropManager.AddDragOverHandler(xTreeView, OnDragOver, true) 
        DragDropManager.AddDragOverHandler(xTreeView, OnDragOver, True) 

    RadTreeView handles internally the DragDropManager events and in order to invoke a custom handler, you need to explicitly specify that you're adding a handler that should be invoked even for already handled events. This is done through the last - bool argument of the DragDropManager.AddDragOverHandler extension method.

  • In the event handler you should use the following code:

        private void OnDragOver(object sender, Telerik.Windows.DragDrop.DragEventArgs e) 
            var options = DragDropPayloadManager.GetDataFromObject(e.Data, TreeViewDragDropOptions.Key) as TreeViewDragDropOptions; 
            if (options!=null && options.DropPosition != Telerik.Windows.Controls.DropPosition.Inside) 
                options.DropPosition = Telerik.Windows.Controls.DropPosition.Inside; 
        Private Sub OnDragOver(sender As Object, e As Telerik.Windows.DragDrop.DragEventArgs) 
            Dim options = TryCast(DragDropPayloadManager.GetDataFromObject(e.Data, TreeViewDragDropOptions.Key), TreeViewDragDropOptions) 
            If options IsNot Nothing AndAlso options.DropPosition <> Telerik.Windows.Controls.DropPosition.Inside Then 
                options.DropPosition = Telerik.Windows.Controls.DropPosition.Inside 
            End If 
        End Sub 

    Please note that if you want the changes applied on the TreeViewDragDropOptions object to affect the DragVisual element, you have to invoke the UpdateDragVisual() method.

Find a runnable project of the previous example in the WPF Samples GitHub repository.

See Also

In this article