关于MV-VM和命令绑定(RelayCommand)有很多讨论,但在与MV-VM模式中绑定到处理程序的路由事件中没有很多讨论。我想找到什么是最好的方法。

这是使用自定义事件的RoutedEvent绑定的示例,也是VM绑定事件处理程序的示例。

<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
}

这只是从命令绑定中的许多示例中获取的概念。我将如何为路由事件工作。

有帮助吗?

解决方案

你可以退房 本文 作者实施类似语法的地方:

<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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top