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

WPF Drag and Drop - Get original source info from DragEventArgs(WPF 拖放 - 从 DragEventArgs 获取原始源信息)
本文介绍了WPF 拖放 - 从 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);
    }
}

任何帮助将不胜感激.

谢谢!

推荐答案

我在另一个问题中找到了答案

这样做的方法是将源ListView传递给DragDrow.DoDragDrop方法即.

The way to do it is to pass the source ListView into the DragDrow.DoDragDrop method ie.

在处理ListView的PreviewMouseMove的方法中做-

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 获取原始源信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)