如何使用 TreeViewDragDropTarget MVVM

发布于 2024-10-04 10:30:48 字数 563 浏览 4 评论 0原文

TreeViewDragDropTarget 似乎涉及很多“幕后魔法”。

到目前为止我看到的所有帖子都停留在树视图的演示上,没有解释 TreeView 绑定的数据收集发生了什么。

TreeViewDragDropTargt 如何与我的 TreeView 绑定到的 ViewModel 属性进行通信?是否有任何关于 TreeView 拖放 ala MVVM 的面向 MVVM 的演练或教程?

更新: "如果将项目拖放到拖放目标上,如果嵌套控件绑定到 ObservableCollection(或任何实现 INotifyCollectionChanged 并包含与删除的项相同类型的项的集合),

则它会添加到嵌套控件中。” 这意味着我可以挂钩 ObservableCollection 的 CollectionChanged 事件,我的 TreeView 必然保持我的模型同步。

有更好的选择吗?

There seems to be a lot of "behind the scenes magic" involved in TreeViewDragDropTarget.

All the posts I've seen so far, stop at the presentation of the treeview without explaining what's going on with collection of data the TreeView is bound to.

How does the TreeViewDragDropTargt communicate back to the ViewModel property that my TreeView is bound to? Are there any MVVM oriented walkthroughs or tutorials about TreeView Drag and Drop ala MVVM?

UPDATE:
"If an item is dropped onto the drag drop target, it is added to the nested control if the nested control is bound to an ObservableCollection (or any collection that implements INotifyCollectionChanged and contains the same type of items as the item that was dropped)."

And what that means is that I can hook into the CollectionChanged event of the ObservableCollection my TreeView is bound to keep my model in sync.

Is there a better option?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久而酒知 2024-10-11 10:30:49

到目前为止我还没有看到更好的选择。请记住,MVC/MVP/MVVM 模式背后的全部要点是测试尽可能多的代码。

对于拖/放,我使用了演示文稿 ->代码隐藏-> Messenger 发出消息“已添加项目”...并从 ViewModel 捕获该事件。

例如,如果隐藏代码中有自定义逻辑,例如验证“源”是否具有有效状态或正确类型等,您可以在“服务”(依赖注入)中委托此检查专门处理这些奇怪的案件。隐藏代码变得最少,并且“拖动服务”变得可测试。

您是否真的需要“拖动服务”,如果是这样,您应该为每个 ViewModel 提供一个“拖动服务”,还是为每个应用程序提供一个“拖动服务”?我会用你的经典答案“这取决于”来回答。

I haven't seen a better option so far. Remember that the whole point behind the MVC/MVP/MVVM patterns is to put as much code as possible under tests.

For Drag/Drop, I've used the Presentation -> Code Behind -> Messenger Raise message "Items Added" ... and catch the event from the ViewModel.

If there's custom logic in the Code Behind, for example, validate that the "source" is of a valid status for example, or of the right type etc, you could delegate this checking in a "service" (Dependency Injected) that would be specialized in handling these odd cases. The Code Behind becomes miminal, and the "drag service" becomes testable.

Do you need really need a "Drag Service", and if so, should you have one "Drag Service" per ViewModel, or one per application ? I'll answer with your classic answer "It Depends".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文