MVVM EventBinding
allows the binding of events (directly in a window’s or page’s XAML code) to commands specified in the Viewmodel. This helps to follow the MVVM pattern and replace event handlers defined in a window’s code-behind by Viewmodel commands. In addition, the event arguments are automatically forwarded to the Viewmodel and are getting passed as the command’s CommandParameter.
This package consists of two .NET libraries – one for .NET 4.5 (to be used in WPF applications) and one for Silverlight applications – that all contain an implementation of the EventBindingExtension
class. When installing the package via Nuget, the correct library is automatically chosen and added to the project. The package can not be installed to projects of different types than .NET 4.5+ or Silverlight 5+.
The package is released under the Apache License 2.0. The package is available on Nuget (https://www.nuget.org/packages/MvvmEventBinding/). To install MVVM EventBinding
, run the following command in the Package Manager Console:
PM> Install-Package MvvmEventBinding
Usage Examples:
Registration of the library’s namespace within the Window’s / Page’s / UserControl’s / etc. header:
<Window | UserControl | Page | ...> x:Class="..." xmlns:mvvm="clr-namespace:MvvmEventBinding;assembly=MvvmEventBinding" ...other attributes go here...> </Window | UserControl | Page | ...>
Usage examples for WPF:
The EventBinding
markup extension is specified as content of any event, similar to the binding of conventional commands. The name of the desired command (that shall be invoked when the event is raised) must be provided as parameter:
<Button Click="{mvvm:EventBinding ButtonClickCommand}">Click me!</Button>
<TextBlock MouseMove="{mvvm:EventBinding TextMoveCommand}">Some text</TextBlock>
<ListView SelectionChanged="{mvvm:EventBinding ListSelectionChangedCommand}" ItemsSource="{Binding Items}" />
Usage examples for Silverlight:
The EventBinding
markup extension is specified as content of any event, similar to the binding of conventional commands. The name of the desired command (that shall be invoked when the event is raised) must be provided as content of the Command
parameter:
<Button Click="{mvvm:EventBinding Command=ButtonClickCommand}">Click me!</Button>
<TextBlock MouseMove="{mvvm:EventBinding Command=TextMoveCommand}">Some text</TextBlock>
<ListBox SelectionChanged="{mvvm:EventBinding Command=ListSelectionChangedCommand}" ItemsSource="{Binding Items}" />
Command implementation in the Viewmodel:
In the Viewmodel, any ICommand
implementation can be used as target for the EventBinding
. Assuming DelegateCommand
is an ICommand implementation that is present in the project, an example could look as follows:
public DelegateCommand ButtonClickCommand { get; set; } public MyViewmodel() { ButtonClickCommand = new DelegateCommand(ButtonClick); } private void ButtonClick(object args) { // do whatever you want with args... }
That’s it – nothing to add in code-behind! The event arguments of whichever type the original event is, are passed to the final command method as parameter.
In addition, it’s also possible to define a CanExecute
condition for the target command, in which case the command will only be invoked if the CanExecute
condition resolves to true
the moment the source event is raised.