我有兩個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);
}
}
任何幫助和提示讚賞。