参考自: http://www.cnblogs.com/chenkai/archive/2010/05/26/1744226.html#1876533 and
http://www.silverlight.net/content/samples/sl4/toolkitcontrolsamples/run/default.html
xmal
<
UserControl
x:Class
="System.Windows.Controls.Samples.DragAndDropSample"
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"
xmlns:win ="clr-namespace:System.Windows;assembly=System.Windows.Controls"
xmlns:controls ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:controlsToolkit ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
xmlns:chartingToolkit ="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:layoutToolkit ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Layout.Toolkit"
>
< Grid x:Name ="LayoutRoot" Background ="White" >
< Grid.RowDefinitions >
< RowDefinition Height ="20" />
< RowDefinition Height ="400" />
</ Grid.RowDefinitions >
< Grid.ColumnDefinitions >
< ColumnDefinition Width ="400" />
< ColumnDefinition Width ="400" />
</ Grid.ColumnDefinitions >
< TextBlock Text ="All Employees" Margin ="0,0,262,0" Width ="138" />
< controlsToolkit:ListBoxDragDropTarget Grid.Column ="0" Grid.Row ="1" AllowDrop ="True" HorizontalContentAlignment ="Stretch" VerticalContentAlignment ="Stretch" x:Name ="first" >
< ListBox x:Name ="fromListBox" SelectionMode ="Extended" DisplayMemberPath ="Name" Height ="396" Width ="396" >
< ListBox.ItemsPanel >
< ItemsPanelTemplate >
< StackPanel Orientation ="Vertical" />
</ ItemsPanelTemplate >
</ ListBox.ItemsPanel >
</ ListBox >
</ controlsToolkit:ListBoxDragDropTarget >
< TextBlock Text ="Organization Hierarchy" Grid.Column ="1" Grid.Row ="0" />
< controlsToolkit:ListBoxDragDropTarget Grid.Column ="1" Grid.Row ="1" AllowDrop ="true" HorizontalContentAlignment ="Stretch" VerticalContentAlignment ="Stretch" x:Name ="two" >
< ListBox x:Name ="fromListBox1" SelectionMode ="Extended" DisplayMemberPath ="Name" Height ="396" Width ="396" >
< ListBox.ItemsPanel >
< ItemsPanelTemplate >
< StackPanel Orientation ="Vertical" />
</ ItemsPanelTemplate >
</ ListBox.ItemsPanel >
</ ListBox >
</ controlsToolkit:ListBoxDragDropTarget >
< Button Content ="Button" Grid.RowSpan ="2" Height ="23" HorizontalAlignment ="Left" Margin ="208,0,0,0" Name ="button1" VerticalAlignment ="Top" Width ="75" Click ="button1_Click" />
</ Grid >
</ UserControl >
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"
xmlns:win ="clr-namespace:System.Windows;assembly=System.Windows.Controls"
xmlns:controls ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:controlsToolkit ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
xmlns:chartingToolkit ="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:layoutToolkit ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Layout.Toolkit"
>
< Grid x:Name ="LayoutRoot" Background ="White" >
< Grid.RowDefinitions >
< RowDefinition Height ="20" />
< RowDefinition Height ="400" />
</ Grid.RowDefinitions >
< Grid.ColumnDefinitions >
< ColumnDefinition Width ="400" />
< ColumnDefinition Width ="400" />
</ Grid.ColumnDefinitions >
< TextBlock Text ="All Employees" Margin ="0,0,262,0" Width ="138" />
< controlsToolkit:ListBoxDragDropTarget Grid.Column ="0" Grid.Row ="1" AllowDrop ="True" HorizontalContentAlignment ="Stretch" VerticalContentAlignment ="Stretch" x:Name ="first" >
< ListBox x:Name ="fromListBox" SelectionMode ="Extended" DisplayMemberPath ="Name" Height ="396" Width ="396" >
< ListBox.ItemsPanel >
< ItemsPanelTemplate >
< StackPanel Orientation ="Vertical" />
</ ItemsPanelTemplate >
</ ListBox.ItemsPanel >
</ ListBox >
</ controlsToolkit:ListBoxDragDropTarget >
< TextBlock Text ="Organization Hierarchy" Grid.Column ="1" Grid.Row ="0" />
< controlsToolkit:ListBoxDragDropTarget Grid.Column ="1" Grid.Row ="1" AllowDrop ="true" HorizontalContentAlignment ="Stretch" VerticalContentAlignment ="Stretch" x:Name ="two" >
< ListBox x:Name ="fromListBox1" SelectionMode ="Extended" DisplayMemberPath ="Name" Height ="396" Width ="396" >
< ListBox.ItemsPanel >
< ItemsPanelTemplate >
< StackPanel Orientation ="Vertical" />
</ ItemsPanelTemplate >
</ ListBox.ItemsPanel >
</ ListBox >
</ controlsToolkit:ListBoxDragDropTarget >
< Button Content ="Button" Grid.RowSpan ="2" Height ="23" HorizontalAlignment ="Left" Margin ="208,0,0,0" Name ="button1" VerticalAlignment ="Top" Width ="75" Click ="button1_Click" />
</ Grid >
</ UserControl >
c#
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace System.Windows.Controls.Samples
{
public partial class DragAndDropSample : UserControl
{
public DragAndDropSample()
{
InitializeComponent();
fromListBox.ItemsSource = PersonDataProvider.GetData();
}
private void button1_Click( object sender, RoutedEventArgs e)
{
ItemCollection tc = fromListBox1.Items;
}
}
public class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
public class PersonDataProvider
{
public static ObservableCollection < Person > GetData()
{
return new ObservableCollection < Person >
{
new Person { Name = " Akash Sharma " },
new Person { Name = " Vinay Sen " },
new Person { Name = " Lalit Narayan " },
new Person { Name = " Madhumita Chatterjee " },
new Person { Name = " Priyanka Patil " },
new Person { Name = " Kumar Sanu " }
};
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace System.Windows.Controls.Samples
{
public partial class DragAndDropSample : UserControl
{
public DragAndDropSample()
{
InitializeComponent();
fromListBox.ItemsSource = PersonDataProvider.GetData();
}
private void button1_Click( object sender, RoutedEventArgs e)
{
ItemCollection tc = fromListBox1.Items;
}
}
public class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
public class PersonDataProvider
{
public static ObservableCollection < Person > GetData()
{
return new ObservableCollection < Person >
{
new Person { Name = " Akash Sharma " },
new Person { Name = " Vinay Sen " },
new Person { Name = " Lalit Narayan " },
new Person { Name = " Madhumita Chatterjee " },
new Person { Name = " Priyanka Patil " },
new Person { Name = " Kumar Sanu " }
};
}
}
}