使用ScatterView在MVVM中拖放 [英] Drag and Drop in MVVM with ScatterView

查看:288
本文介绍了使用ScatterView在MVVM中拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在使用MVVM模式构建的Surface应用程序中实现拖放功能。我正在努力提出一种实现这一点的手段,同时遵守MVVM模式。虽然我试图在Surface应用程序中执行此操作,但我认为该解决方案也适用于WPF。

I'm trying to implement drag and drop functionality in a Surface Application that is built using the MVVM pattern. I'm struggling to come up with a means to implement this while adhering to the MVVM pattern. Though I'm trying to do this within a Surface Application I think the solution is general enough to apply to WPF as well.

我正在尝试生成以下功能:

I'm trying to produce the following functionality:


  • 用户联系ScatterViewItem中的FrameworkElement开始拖动操作(ScatterViewItem的特定部分启动拖放功能) / li>
  • 当拖动操作开始时,会创建该ScatterViewItem的副本并将其强加在原始的ScatterViewItem上,该副本是用户拖动的副本,最终将会丢弃

  • 用户可以将该项目拖放到另一个ScatterViewItem(放置在单独的ScatterView中)

整体交互与ShoppingCart应用程序非常相似在Surface SDK中提供,除了源对象包含在ScatterView而不是ListBox中。

The overall interaction is quite similar to the ShoppingCart application provided in the Surface SDK, except that the source objects are contained within a ScatterView rather than a ListBox.

我不确定如何进行以启用正确的通信betwe我的ViewModels为了提供这个功能。我遇到的主要问题是当用户联系FrameworkElement时复制ScatterViewItem。

I'm unsure how to proceeded in order to enable the proper communication between my ViewModels in order to provide this functionality. The main issue I've encountered is replicating the ScatterViewItem when the user contacts the FrameworkElement.

推荐答案

您可以使用附加的属性。创建一个附加的属性,并在setproperty方法绑定到下拉的事件:

You could use an attached property. Create an attached property and in the setproperty method bind to the droped event :


public static void SetDropCommand(ListView source, ICommand command)
        {
            source.Drop += (sender, args) =>
                               {
                                   var data = args.Data.GetData("FileDrop");
                                   command.Execute(data);
                               };
        }

然后,您可以将视图模型中的命令绑定到相关控件上视图。显然,您可能希望使附加的财产适用于您的特定控制类型而不是列表视图。

Then you can bind a command in your view model to the relevant control on the view. Obviously you may want to make your attached property apply to your specific control type rather than a listview.

希望有所帮助。

这篇关于使用ScatterView在MVVM中拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆