关于 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/