Search Wiki:
Resource Page Description
Provides a utility class making it easy to execute commands when events are raised on WPF elements.

Further details can be found on Samuel Jack's blog!

Using the EventBehaviourFactory


First of all, you need to include my EventBehaviourFactory class in your project. You can get this from the source code in the Dowloads section.

Then you need to define a static class to hold a new Attached property that you will attach to an object to specify which command to execute when a particular event is raised. You need to call EventBehaviourFactory.CreateCommandExecutionEventBehaviour and pass it the routed event that you want to handle - note you must pass the static field holding the RoutedEvent object (usually the same name as the event itself, but ending in "Event") not the event property. Just as when creating a standard attached property, you also need to pass the name of the property as a string (this should match the name of the static field that you store the DependencyProperty in), and the type of the class that is defining the property.

As an example, I'll create a property that will execute a Command when the TextChanged event of a TextBox is raised:

using System.Windows;   
using System.Windows.Controls;   
using System.Windows.Input;   
  
namespace FunctionalFun.Wpf   
{   
    public static class TextBoxBehaviour   
    {   
        public static readonly DependencyProperty TextChangedCommand = EventBehaviourFactory.CreateCommandExecutionEventBehaviour(
                                                                                                              TextBox.TextChangedEvent, "TextChangedCommand", typeof (TextBoxBehaviour));   
           
        public static void SetTextChangedCommand(DependencyObject o, ICommand value)   
        {   
            o.SetValue(TextChangedCommand, value);   
        }   
  
        public static ICommand GetTextChangedCommand(DependencyObject o)   
        {   
            return o.GetValue(TextChangedCommand) as ICommand;   
        }   
    }   
}  

The important part is in line 9, where I'm calling EventBehaviourFactory. This is the part that creates the attached behaviour. In the download, I've included a Reshaper template to mostly automate this step.

To make use of the property you just set a value for it on the object whose events interest you, binding it to the appropriate command. Here's an example:

<Window x:Class="FunctionalFun.Wpf.Window1"  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    xmlns:ff="clr-namespace:FunctionalFun.Wpf"  
    Title="Window1" Height="300" Width="300">  
    <Window.DataContext>  
        <ff:ViewModel/>  
    </Window.DataContext>  
    <Grid>  
        <Grid.RowDefinitions>  
            <RowDefinition/>  
        </Grid.RowDefinitions>  
        <TextBox ff:TextBoxBehaviour.TextChangedCommand="{Binding TextChanged}"/>  
    </Grid>  
</Window>  

The ViewModel that this Window uses (initialised in line 7) is:

using System;   
using System.Collections.Generic;   
using System.Linq;   
using System.Text;   
using System.Windows;   
using System.Windows.Input;   
  
namespace FunctionalFun.Wpf   
{   
    public class ViewModel   
    {   
        public ICommand TextChanged   
        {   
            get  
            {   
                //  this is very lazy: I should cache the command!   
                return new TextChangedCommand();   
            }   
        }   
  
        private class TextChangedCommand : ICommand   
        {   
            public event EventHandler CanExecuteChanged;   
            public void Execute(object parameter)   
            {   
                MessageBox.Show("Text Changed");   
            }   
  
            public bool CanExecute(object parameter)   
            {   
                return true;   
            }   
        }   
    }   
}  
Last edited Oct 16 2008 at 4:40 PM  by samuel_d_jack, version 5
Updating...
Page view tracker