我正在开发一个应用程序的第二个版本,作为重写的一部分,我必须转移到MVVM架构。我正承受着把每一位代码都放在视图模型类中的压力--在代码隐藏文件中使用c#是不受欢迎的。(我知道,我知道......我明白代码隐藏不是一件坏事,但这次不是我说了算)。
对于实现命令接口的对象来说,这很容易。我已经找到了大量关于如何将这些对象的命令绑定到视图模型中的ICommand的信息。问题是对于没有这个接口的对象,例如。
<ListBox
x:Name="myListBox"
MouseDoubleClick="myCallbackFunction">
<!-- ... -->
</ListBox>
我想知道如何将列表框的MouseDoubleClick事件绑定到视图模型中实现的myCallbackFunction,这可能吗?
谢谢!
5条答案
按热度按时间anauzrmj1#
这是不可能直接实现的,可以通过Attached Property或Behavior来实现,尽管找到并调用合适的方法仍然有点棘手(这可以通过Reflection相当容易地实现)。
也就是说,这通常是通过
ICommand
处理的-例如,MVVM Light有一个很好的EventToCommand行为,可以将任何事件Map到ViewModel上的ICommand。使用ICommand的好处是,您仍然可以使用DataBinding,因为ICommand是作为属性公开的。a0x5cqrl2#
从.NET 4.5开始,WPF支持事件的标记扩展。使用该功能,我实现了一个多功能的方法绑定扩展,并在此处介绍了它:
http://www.singulink.com/CodeIndex/post/building-the-ultimate-wpf-event-method-binding-extension
可用于绑定到使用完整属性路径语法的方法,支持绑定和其他标记扩展作为参数,并自动路由到与所提供参数的签名匹配的方法。下面是一些用法示例:
查看模型方法签名:
xoefb8l83#
要直接回答您的问题,请参阅Why to avoid the codebehind in WPF MVVM pattern?它建议您可能需要的两个东西。
但是,为什么要将ListBox的MouseDoubleClick绑定到视图模型中的ICommand?
另一种方法是在代码隐藏中编写一个方法来注册MouseDoubleClick。由于以下事实,这并不坏。
1.有意义的数据绑定是视图和视图模型之间的交互。例如,当用户向TextBox输入一些文本时,视图模型也会更新。相反,如果视图模型从数据库获取数据,则该数据将显示在视图中。但是,视图模型中的ICommand与视图绑定的情况并非如此。
1.当然,ICommand的CanExecute对视图模型很重要,但在许多情况下,它与视图模型无关或不受关注。在这种情况下,ICommand绑定和编写代码隐藏之间的区别在于MouseDoubleClick事件与ICommand绑定或向事件处理程序注册的位置。
e7arh2l64#
一种方法是在代码后处理事件,并从代码后调用视图模型的适当方法
你也可以去一些现成的命令库,如this教程使用ACB
cetgtptt5#
尝试EventBinder,它允许您将方法直接绑定到任何事件,包括您自己的事件,而无需将方法 Package 在ICommand容器中。
https://github.com/Serg046/EventBinder
https://www.nuget.org/packages/EventBinder
支持. NET框架3.0+、. NET核心3.0+和Avalonia。
特点:
.
分隔符、属性和字段绑定到嵌套对象$
符号和位置编号($0
、$1
等)传递事件参数{Binding}
作为参数传递用法:
装订:
或