Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF拖放-从DragEventArgs获取原始源信息_C#_Wpf_Mvvm_Drag And Drop - Fatal编程技术网

C# WPF拖放-从DragEventArgs获取原始源信息

C# WPF拖放-从DragEventArgs获取原始源信息,c#,wpf,mvvm,drag-and-drop,C#,Wpf,Mvvm,Drag And Drop,我正在尝试使用MVVM编写拖放功能,这将允许我将PersonModel对象从一个ListView拖动到另一个 这几乎是工作,但我需要能够从DragEventArgs获得源ListView的ItemsSource,我不知道如何做 private void OnHandleDrop(DragEventArgs e) { if (e.Data != null && e.Data.GetDataPresent("myFormat")) { var pers

我正在尝试使用
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);
    }
}
handledrop上的私有无效(DragEventArgs e)
{
if(e.Data!=null&&e.Data.GetDataPresent(“myFormat”))
{
var person=e.Data.GetData(“myFormat”)作为PersonModel;
//获取源ListView的ItemsSource
..
//获取目标ListView的ItemsSource并将此人添加到其中
((ObservableCollection)((ListView)e.Source.ItemsSource)).Add(person);
}
}
任何帮助都将不胜感激

谢谢

我从中找到了答案

方法是将源ListView传递到DragDrow.DoDragDrop方法ie中

在处理ListView的PreviewMouseMove的方法中-

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方法中将代码更改为

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);
        }
    }
}
handledrop上的私有静态无效(DragEventArgs e)
{
if(e.Data!=null&&e.Data.GetDataPresent(“System.Windows.Controls.ListView”))
{
//var person=e.Data.GetData(“myFormat”)作为PersonModel;
//获取源ListView的ItemsSource并删除此人
var source=e.Data.GetData(“System.Windows.Controls.ListView”)作为ListView;
如果(源!=null)
{
var person=source.SelectedItem作为PersonModel;
((ObservableCollection)source.ItemsSource).删除(person);
//获取目标ListView的ItemsSource
((ObservableCollection)((ListView)e.Source.ItemsSource)).Add(person);
}
}
}

在我的拖放实现中,我创建了类DragManager(即singleton),并添加了一个私有字段draggingElement。因为一次只能拖动一个元素。