WPF MVVM Routed Events
There is a lot of discussion about MV-VM and command binding (RelayCommand), but not much has to do with binding Routed Events to a handler in the MV-VM pattern. I want to find the best approach.
Here is an example of a RoutedEvent binding using a custom event and an associated event handler for a virtual machine.
<Navigation:LeftNavigation x:Name="_leftNav" Margin="3"
BindingHelper:EventHelper.RoutedEvent="Events:Customer.SelectionChanged"
BindingHelper:EventHelper.EventHandler="{Binding SelectionChanged}" />
In my Vm, I would have an event handler similar to this one.
public void SelectionChanged(object sender, CustomerSelectionChangedArgs e)
{
// Do something
}
This is just a concept taken from many examples from command binding. How can I get this to work for routed events.
+2
source to share
1 answer
You can check this article where the author implements a similar syntax:
<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>
+2
source to share