问题描述
目前我的 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 原则的情况下处理拖放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)