How to handle drag/drop without violating MVVM principals?(如何在不违反 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 中处理拖放操作而不在 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 原则的情况下处理拖放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在不违反 MVVM 原则的情况下处理拖放?


基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01