向外拖动的增加一个PreviewMouseLeftButtonDown事件
<ListBox ItemTemplate="{DynamicResource HoleListTemplate}" PreviewMouseLeftButtonDown="ListBox_MouseDown" ItemsSource="{Binding PGHoles}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<local:UniformGridWithOrientation Orientation="Horizontal" Columns="8" Rows="10"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
拖动到的那个ListBox使用AllowDrop属性改为True,增加Drop事件
<ListBox ItemTemplate="{DynamicResource ChipListTemplate}" Margin="10" AllowDrop="True" Drop="ListBox_Drop" ItemsSource="{Binding PGChips2}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<local:UniformGridWithOrientation Orientation="Horizontal" Columns="4" Rows="4"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
后台事件
private void ListBox_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(PGHole)))
{
PGHole pGHole = e.Data.GetData(typeof(PGHole)) as PGHole;
var listBoxItem = VisualUpwardSearch<ListBoxItem>(e.OriginalSource as DependencyObject) as ListBoxItem;
PGChip pGChip = (PGChip)listBoxItem.DataContext;
pGChip.HoleIndex = pGHole.HoleIndex;
}
}
private void ListBox_MouseDown(object sender, MouseButtonEventArgs e)
{
var listBoxItem = VisualUpwardSearch<ListBoxItem>(e.OriginalSource as DependencyObject) as ListBoxItem;
PGHole pGHole = (PGHole)listBoxItem.DataContext;
DragDrop.DoDragDrop(listBoxItem, pGHole, DragDropEffects.Copy);
}