WPF MVVM 路由事件

标签 wpf mvvm binding routed-events

关于 M-V-VM 和命令绑定(bind) (RelayCommand) 的讨论很多,但在 M-V-VM 模式中将路由事件绑定(bind)到处理程序的讨论不多。我想找到最好的方法。

下面是一个使用自定义事件和绑定(bind)到 VM 的事件处理程序的 RoutedEvent 绑定(bind)示例。

<Navigation:LeftNavigation x:Name="_leftNav" Margin="3"
            BindingHelper:EventHelper.RoutedEvent="Events:Customer.SelectionChanged"
            BindingHelper:EventHelper.EventHandler="{Binding SelectionChanged}" />

在我的 Vm 中,我会有一个与此类似的事件处理程序。
public void SelectionChanged(object sender, CustomerSelectionChangedArgs e)
{
    // Do something
}

这只是从命令绑定(bind)的许多示例中得出的一个概念。我如何让它适用于路由事件。

最佳答案

您可以查看this article作者实现了类似语法的地方:

<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" x:Name="test">
  <local:CommandBehaviorCollection.Behaviors>
    <local:BehaviorBinding Event="MouseLeftButtonDown" Action="{Binding DoSomething}" CommandParameter="An Action on MouseLeftButtonDown"/>
    <local:BehaviorBinding Event="MouseRightButtonDown" Command="{Binding SomeCommand}" CommandParameter="A Command on MouseRightButtonDown"/>
  </local:CommandBehaviorCollection.Behaviors>
  <TextBlock Text="MouseDown on this border to execute the command"/>
</Border>

关于WPF MVVM 路由事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1586229/

相关文章:

java - 更新后刷新JTable

xaml - Windows 8. Metro 应用程序。 XAML 异步绑定(bind)

WPF:如何使按钮背景透明

c# - Caliburn.Micro:DataContext 上属性的调用方法

c# - 在组标题 WPF 中显示 "filled"行的数量

java - 无法编辑或更新 Room 数据库条目

java - JBoss 绑定(bind) IP 地址

c# - C# 中的字符串复数化和本地化

wpf - 数据绑定(bind)值未从其他线程更新

c# - 如何创建类似于 Google Images 的 WPF 布局