汉鼎拖/放不违反MVVM校长? [英] Handing drag/drop without violating MVVM principals?
问题描述
目前我在我的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屋!