【发布时间】:2016-06-20 16:56:09
【问题描述】:
我有两个ListBoxes,它们的ItemsSource 绑定到单独的ObservableCollection<ICustomObject>。 ICustomObject 是为不同类型的CustomObject 定义一些基本属性的接口。
我希望一个ListBox 成为可能元素的静态、不变源,用户可以将它们多次拖动到另一个ListBox。目标中的元素也应该是可重新排列的。
结果应该是一个工具箱,用户可以从中构建一个由多个CustomObject组成的文档。
我为此使用GongSolutions.WPF.DragDrop 库,提交c680fcf。
<ListBox ItemsSource="{Binding AvailableElements}" dd:DragDrop.IsDragSource="True" dd:DragDrop.DragDropCopyKeyState="ControlKey">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox ItemsSource="{Binding SelectedElements}" dd:DragDrop.IsDropTarget="True" dd:DragDrop.IsDragSource="True">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
有了这个,我可以通过按住 ControlKey 将元素从源复制到目标。
但是这样做有两个问题:
- 有什么方法可以默认复制操作,这样就不需要按键了?
- 如何进行真实副本?目前,目标列表的所有列表元素都指向同一个对象,因此无法更改单个元素的属性。
我已经尝试过使用自定义 DropHandler 但这使得重新排序元素变得不可能:
public void Drop(IDropInfo dropInfo)
{
IFormElement data = Activator.CreateInstance(dropInfo.Data.GetType()) as IFormElement;
if (data != null)
{
SelectedElements.Add(data);
}
}
感谢任何帮助和提示。
【问题讨论】:
标签: c# wpf mvvm drag-and-drop