问题描述
目前我的 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 中处理拖放操作而不在 View 上添加任何依赖项?
How can I handle drag/drop in my ViewModel without adding any dependencies on the View?
推荐答案
有这样的库,例如 gong 以及各种博客文章中的类似片段.
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 并调用视图模型上的方法.
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 原则的情况下处理拖放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!