How to: Enable Drag-and-Drop in the TreeListView
- 7 minutes to read
This example demonstrates how to enable Drag-and-Drop functionality in the GridControl with a TreeList View. The DataViewBase.AllowDragDrop property is set to true.
Note
The GridControl ships with native drag-and-drop support starting from version 17.2. Use Drag-and-Drop Managers to enable drag-and-drop functionality in the previous versions.
The image below shows the result:
<Application x:Class="How_to_TreeList_Drag_and_Drop.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
Imports System.Windows
Imports System.Collections.ObjectModel
Namespace How_to_TreeList_Drag_and_Drop
Partial Public Class MainWindow
Inherits Window
Public Class Employee
Public Property ID() As Integer
Public Property ParentID() As Integer
Public Property Name() As String
Public Property Position() As String
Public Property Department() As String
Public Overrides Function ToString() As String
Return Name
End Function
End Class
Public NotInheritable Class Stuff
Private Sub New()
End Sub
Public Shared Function GetStuff() As ObservableCollection(Of Employee)
Dim stuff_Renamed As New ObservableCollection(Of Employee)()
stuff_Renamed.Add(New Employee() With { _
.ID = 1, _
.ParentID = 0, _
.Name = "Gregory S. Price", _
.Department = "", _
.Position = "President" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 2, _
.ParentID = 1, _
.Name = "Irma R. Marshall", _
.Department = "Marketing", _
.Position = "Vice President" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 3, _
.ParentID = 1, _
.Name = "John C. Powell", _
.Department = "Operations", _
.Position = "Vice President" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 4, _
.ParentID = 1, _
.Name = "Christian P. Laclair", _
.Department = "Production", _
.Position = "Vice President" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 5, _
.ParentID = 1, _
.Name = "Karen J. Kelly", _
.Department = "Finance", _
.Position = "Vice President" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 6, _
.ParentID = 2, _
.Name = "Brian C. Cowling", _
.Department = "Marketing", _
.Position = "Manager" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 7, _
.ParentID = 2, _
.Name = "Thomas C. Dawson", _
.Department = "Marketing", _
.Position = "Manager" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 8, _
.ParentID = 2, _
.Name = "Angel M. Wilson", _
.Department = "Marketing", _
.Position = "Manager" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 9, _
.ParentID = 2, _
.Name = "Bryan R. Henderson", _
.Department = "Marketing", _
.Position = "Manager" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 10, _
.ParentID = 3, _
.Name = "Harold S. Brandes", _
.Department = "Operations", _
.Position = "Manager" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 11, _
.ParentID = 3, _
.Name = "Michael S. Blevins", _
.Department = "Operations", _
.Position = "Manager" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 12, _
.ParentID = 3, _
.Name = "Jan K. Sisk", _
.Department = "Operations", _
.Position = "Manager" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 13, _
.ParentID = 3, _
.Name = "Sidney L. Holder", _
.Department = "Operations", _
.Position = "Manager" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 14, _
.ParentID = 4, _
.Name = "James L. Kelsey", _
.Department = "Production", _
.Position = "Manager" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 15, _
.ParentID = 4, _
.Name = "Howard M. Carpenter", _
.Department = "Production", _
.Position = "Manager" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 16, _
.ParentID = 4, _
.Name = "Jennifer T. Tapia", _
.Department = "Production", _
.Position = "Manager" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 17, _
.ParentID = 5, _
.Name = "Judith P. Underhill", _
.Department = "Finance", _
.Position = "Manager" _
})
stuff_Renamed.Add(New Employee() With { _
.ID = 18, _
.ParentID = 5, _
.Name = "Russell E. Belton", _
.Department = "Finance", _
.Position = "Manager" _
})
Return stuff_Renamed
End Function
End Class
Public Sub New()
InitializeComponent()
gridControl.ItemsSource = Stuff.GetStuff()
End Sub
End Class
End Namespace
' Developer Express Code Central Example:
' How to enable Drag and Drop feature in the TreeList
'
' This example shows how to enable Drag and Drop functionality in the TreeList
' View.
'
' You can find sample updates and versions for different programming languages here:
' https://supportcenter.devexpress.com/ticket/details/e3917/how-to-enable-drag-and-drop-feature-in-the-treelistview
Imports System
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Windows
Namespace How_to_TreeList_Drag_and_Drop
''' <summary>
''' Interaction logic for App.xaml
''' </summary>
Partial Public Class App
Inherits Application
End Class
End Namespace
<Window x:Class="How_to_TreeList_Drag_and_Drop.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
Title="MainWindow" Height="350" Width="525">
<Grid>
<dxg:GridControl Name="gridControl" SelectionMode="Row">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Name" ReadOnly="True"/>
<dxg:GridColumn FieldName="Department" ReadOnly="True"/>
<dxg:GridColumn FieldName="Position" ReadOnly="True"/>
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TreeListView KeyFieldName="ID" ParentFieldName="ParentID"
AutoExpandAllNodes="True" AllowDragDrop="True" />
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</Window>
using System.Windows;
using System.Collections.ObjectModel;
namespace How_to_TreeList_Drag_and_Drop {
public partial class MainWindow : Window {
public class Employee {
public int ID { get; set; }
public int ParentID { get; set; }
public string Name { get; set; }
public string Position { get; set; }
public string Department { get; set; }
public override string ToString() {
return Name;
}
}
public static class Stuff {
public static ObservableCollection<Employee> GetStuff() {
ObservableCollection<Employee> stuff = new ObservableCollection<Employee>();
stuff.Add(new Employee() { ID = 1, ParentID = 0, Name = "Gregory S. Price", Department = "", Position = "President" });
stuff.Add(new Employee() { ID = 2, ParentID = 1, Name = "Irma R. Marshall", Department = "Marketing", Position = "Vice President" });
stuff.Add(new Employee() { ID = 3, ParentID = 1, Name = "John C. Powell", Department = "Operations", Position = "Vice President" });
stuff.Add(new Employee() { ID = 4, ParentID = 1, Name = "Christian P. Laclair", Department = "Production", Position = "Vice President" });
stuff.Add(new Employee() { ID = 5, ParentID = 1, Name = "Karen J. Kelly", Department = "Finance", Position = "Vice President" });
stuff.Add(new Employee() { ID = 6, ParentID = 2, Name = "Brian C. Cowling", Department = "Marketing", Position = "Manager" });
stuff.Add(new Employee() { ID = 7, ParentID = 2, Name = "Thomas C. Dawson", Department = "Marketing", Position = "Manager" });
stuff.Add(new Employee() { ID = 8, ParentID = 2, Name = "Angel M. Wilson", Department = "Marketing", Position = "Manager" });
stuff.Add(new Employee() { ID = 9, ParentID = 2, Name = "Bryan R. Henderson", Department = "Marketing", Position = "Manager" });
stuff.Add(new Employee() { ID = 10, ParentID = 3, Name = "Harold S. Brandes", Department = "Operations", Position = "Manager" });
stuff.Add(new Employee() { ID = 11, ParentID = 3, Name = "Michael S. Blevins", Department = "Operations", Position = "Manager" });
stuff.Add(new Employee() { ID = 12, ParentID = 3, Name = "Jan K. Sisk", Department = "Operations", Position = "Manager" });
stuff.Add(new Employee() { ID = 13, ParentID = 3, Name = "Sidney L. Holder", Department = "Operations", Position = "Manager" });
stuff.Add(new Employee() { ID = 14, ParentID = 4, Name = "James L. Kelsey", Department = "Production", Position = "Manager" });
stuff.Add(new Employee() { ID = 15, ParentID = 4, Name = "Howard M. Carpenter", Department = "Production", Position = "Manager" });
stuff.Add(new Employee() { ID = 16, ParentID = 4, Name = "Jennifer T. Tapia", Department = "Production", Position = "Manager" });
stuff.Add(new Employee() { ID = 17, ParentID = 5, Name = "Judith P. Underhill", Department = "Finance", Position = "Manager" });
stuff.Add(new Employee() { ID = 18, ParentID = 5, Name = "Russell E. Belton", Department = "Finance", Position = "Manager" });
return stuff;
}
}
public MainWindow() {
InitializeComponent();
gridControl.ItemsSource = Stuff.GetStuff();
}
}
}