【发布时间】:2010-12-23 12:44:14
【问题描述】:
我正在尝试使用MVVM 编写拖放功能,这将允许我将PersonModel 对象从一个ListView 拖到另一个。
这几乎可以工作,但我需要能够从 DragEventArgs 中获取源 ListView 的 ItemsSource,但我不知道该怎么做。
private void OnHandleDrop(DragEventArgs e)
{
if (e.Data != null && e.Data.GetDataPresent("myFormat"))
{
var person = e.Data.GetData("myFormat") as PersonModel;
//Gets the ItemsSource of the source ListView
..
//Gets the ItemsSource of the target ListView and Adds the person to it
((ObservableCollection<PersonModel>)(((ListView)e.Source).ItemsSource)).Add(person);
}
}
任何帮助将不胜感激。
谢谢!
【问题讨论】:
-
在我的拖放实现中,我创建了类 DragManager(即单例)并添加了一个私有字段 draggingElement。因为一次只能拖动一个元素。
标签: c# wpf mvvm drag-and-drop