Introduction
How to drag and drop ListBox Items in Windows Presentation Foundation(WPF) within the parent bounds has been a question that's appeared on many websites. So I have decided to tackle the problem here. To demonstrate drag and drop Listbox items within the parent bounds, I've added Telerik Assembly which contains drag-drop-behavior (<telerik: ListBoxDragDropBehavior/>). For dragging and dropping Listbox items and for restricting the dragged items within the parent bounds an adorner layer can be used. You can select multiple Listbox items by holding the Ctrl or Shift key and then dragging one of the selected items.
Generally, the drag-drop operations are executed in a separate window. The Adorner layer can also be used, which will force the drag visual to remain within the boundaries of the current window. Indeed, the <telerik: ListBoxDragDropBehavior/> Telerik behavior also exposes methods for further customization, and the target framework version is Dot-Net Framework - 4.7.2. Assembly Versions used in the sample is 2020.3.1012.40Telerik.Windows.Control and Telerik.Windows.Control.Navigation.
Getting Started
In this walkthrough, you will create a custom WPF UserControl that represents ListBox Items. You will implement functionality on the control to enable data transfer through telerik-drag-and-drop behavior. For example, if you drag one ListBoxItem to another, the data is copied from the source to the target.
- Creating a WPF Project. Open Visual Studio 2017 or higher version.
- Go to File => New => Project.
- Select Window in installed templates.
- Select WPF Application.
- Enter the Name and choose the location.
- Click OK.
- Create a UserControl and add the Following Xaml Code and XAML.cs code-behind.
- Add telerik reference in xaml.
- xmlns:telerik=http://schemas.telerik.com/2008/xaml/presentation
DragDropCustomControl.xaml
<UserControl
x:Class="DragDropWithinParentBoundaries.CustomControl.DragDropCustomControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:behavior="clr-namespace:DragDropWithinParentBoundaries.DragDropBehavior"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DragDropWithinParentBoundaries.Models"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
d:DesignHeight="150"
d:DesignWidth="400"
mc:Ignorable="d">
<UserControl.Resources>
<local:CountryViewModel x:Key="CountryViewMode"/>
<Style x:Key="DraggableListBoxItem" TargetType="telerik:RadListBoxItem">
<Setter Property="telerik:DragDropManager.AllowCapturedDrag" Value="True"/>
</Style>
</UserControl.Resources>
<Grid DataContext="{StaticResource CountryViewMode}">
<telerik:RadListBox
x:Name="radListObj"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
AllowDrop="True"
ItemContainerStyle="{StaticResource DraggableListBoxItem}"
ItemsSource="{Binding CountryView}"
SelectionMode="Extended">
<telerik:RadListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<TextBlock FontWeight="Bold" Text="{Binding Name}"/>
<TextBlock Grid.Column="1" Text="{Binding Capital}"/>
</Grid>
</DataTemplate>
</telerik:RadListBox.ItemTemplate>
<telerik:RadListBox.DragVisualProvider>
<telerik:ScreenshotDragVisualProvider />
</telerik:RadListBox.DragVisualProvider>
<telerik:RadListBox.DragDropBehavior>
<telerik:ListBoxDragDropBehavior/>
<!--default telerik behaviour -->
</telerik:RadListBox.DragDropBehavior>
</telerik:RadListBox>
</Grid>
</UserControl>
Code Explanation

Join the conversation! Your thoughts help the community grow.