汉鼎拖/放不违反MVVM校长? [英] Handing drag/drop without violating MVVM principals?

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

问题描述

目前我在我的XAML

Currently I have in my XAML

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

在我查看codebehind我所有的拖/放code的存在,而不是我的ViewModel中。

All of my drag/drop code exists within the codebehind of my View, rather than within my 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.

不过,你不要太挂在具有绝对没有code-后面。举例来说,这仍然是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();
}

一个命令绑定可能是一个更好的选择,但其逻辑是绝对的视图模型。随着像拖放,这是要划清界线更多的变数。你可以有code-落后国际$ P $磅的阻力参数数量,并呼吁视图模型方法在适当的时候。

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天全站免登陆