WPF拖放 - 从获取原始DragEventArgs源信息 [英] WPF Drag and Drop - Get original source info from DragEventArgs
问题描述
我试图写拖放使用 MVVM
挂断功能,这将让我拖 PersonModel
从一个对象的ListView
到另一个。
I am trying write Drag and Drop functionality using MVVM
which will allow me to drag PersonModel
objects from one ListView
to another.
这几乎是工作,但我需要能够得到来自DragEventArgs源的ListView我无法弄清楚如何做的的ItemsSource。
This is almost working but I need to be able to get the ItemsSource of the source ListView from the DragEventArgs which I cant figure out how to do.
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);
}
}
任何帮助将不胜感激。
Any help would be greatly appreciated.
感谢
推荐答案
I found the answer in another question
要做到这一点的方法是到源的ListView传递到该DragDrow.DoDragDrop方法即
The way to do it is to pass the source ListView into the DragDrow.DoDragDrop method ie.
在它处理PreviewMouseMove为ListView做的方法 -
In the method which handles the PreviewMouseMove for the ListView do-
private static void List_MouseMove(MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
if (e.Source != null)
{
DragDrop.DoDragDrop((ListView)e.Source, (ListView)e.Source, DragDropEffects.Move);
}
}
}
然后在OnHandleDrop方法修改代码,
and then in the OnHandleDrop method change the code to
private static void OnHandleDrop(DragEventArgs e)
{
if (e.Data != null && e.Data.GetDataPresent("System.Windows.Controls.ListView"))
{
//var person = e.Data.GetData("myFormat") as PersonModel;
//Gets the ItemsSource of the source ListView and removes the person
var source = e.Data.GetData("System.Windows.Controls.ListView") as ListView;
if (source != null)
{
var person = source.SelectedItem as PersonModel;
((ObservableCollection<PersonModel>)source.ItemsSource).Remove(person);
//Gets the ItemsSource of the target ListView
((ObservableCollection<PersonModel>)(((ListView)e.Source).ItemsSource)).Add(person);
}
}
}
这篇关于WPF拖放 - 从获取原始DragEventArgs源信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!