如何处理拖放而不违反MVVM主体? [英] How to handle drag/drop without violating MVVM principals?

查看:128
本文介绍了如何处理拖放而不违反MVVM主体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我在我的XAML

Currently I have in my XAML

<TabControl  
    AllowDrop="True"
    PreviewDragOver="DragOver"
    PreviewDrop="Drop" />

我的所有拖放代码都存在于我的View代码中,而不是在我的ViewModel内。

All of my drag/drop code exists within the codebehind of my View, rather than within my ViewModel.

如何在ViewModel中处理拖放,而不在视图中添加任何依赖项?

How can I handle drag/drop in my ViewModel without adding any dependencies on the View?

推荐答案

有这样的图书馆,例如和类似的各种博客文章中的片段。

There are libraries for this such as gong and similar snippets on various blog articles.

但是,绝对没有代码隐藏,你不应该太挂起。例如,这本书仍然是MVVM:

However, you shouldn't get too hung up on having absolutely no code-behind. For example, this is still MVVM in my book:

void ButtonClicked(object sender, EventArgs e)
{
    ((MyViewModel) this.DataContext).DoSomething();
}

命令绑定可能是一个更好的选择,但逻辑肯定在视图模型。使用拖放功能,您可以在其中绘制线条更加变数。您可以在代码隐藏后解释Drag Args和调用方法在viewmodel适当时。

A command binding might be a better choice, but the logic is definitely in the viewmodel. With something like Drag and Drop, it's more variable where you want to draw the line. You can have code-behind interpret the Drag Args and call methods on the viewmodel when appropriate.

这篇关于如何处理拖放而不违反MVVM主体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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