2012-05-19

Articles

DragAndDropBehavior

 

Do you would like to have a drag and drop possibility between lists? With this behavior it is ready to use. Just define where a list can drop its item to and that's it.

 

alt

 

Usage

<UserControl x:Class="DW.Interactivity.Demo.DragAndDropBehaviorControl"
			 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
			 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
			 xmlns:Interactivity="http://schemas.my-libraries.de/wpf/interactivity">
	<Grid>
		<Grid.ColumnDefinitions>
			<ColumnDefinition />
			<ColumnDefinition />
		</Grid.ColumnDefinitions>
		
		<ListBox ItemsSource="{Binding LeftItems}"
				 AllowDrop="True"
				 x:Name="left"
				 Interactivity:DragAndDropBehavior.DropTo="{Binding ElementName=right}" />

		<ListBox ItemsSource="{Binding RightItems}"
				 AllowDrop="True"
				 x:Name="right"
				 Grid.Column="1"
				 Interactivity:DragAndDropBehavior.DropTo="{Binding ElementName=left}" />
	</Grid>
</UserControl>
using System;
using System.Collections.ObjectModel;
using System.Windows.Controls;

namespace DW.Interactivity.Demo
{
	public partial class DragAndDropBehaviorControl : UserControl
	{
		public DragAndDropBehaviorControl()
		{
			InitializeComponent();
			DataContext = this;

			LeftItems = new ObservableCollection<string>();
			RightItems = new ObservableCollection<string>();

			for (int i = 0; i < 10; ++i)
				LeftItems.Add(Guid.NewGuid().ToString());
		}

		public ObservableCollection<string> LeftItems { get; set; }
		public ObservableCollection<string> RightItems { get; set; }
	}
}