Custom routed events and Commands

Topics: Prism v1
Jan 21, 2009 at 7:56 AM
Is there a way to use commands with custom routed events? for example, this is my user control code:

    public static readonly RoutedEvent AvailableItemSelectedEvent = EventManager.RegisterRoutedEvent(
        "AvailableItemSelected", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(DblListSelector));

    // CLR accessors
    public event RoutedEventHandler AvailableItemSelected
            add { AddHandler(AvailableItemSelectedEvent, value); }
            remove { RemoveHandler(AvailableItemSelectedEvent, value); }

     void RaiseAvailableItemSelectedEvent()
            RoutedEventArgs newEventArgs = new RoutedEventArgs(DblListSelector.AvailableItemSelectedEvent);

    private void avialableListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)

And then in the View that uses the control:



cust:blListSelector x:Name="resolutionSelector"  AvailableItemSelected="{Binding AvailableSourceSelectedCommand}"/>


But then we get the following error:
AvailableItemSelected="{Binding AvailableSourceSelectedCommand}" is not valid. '{Binding AvailableSourceSelectedCommand}' is not a valid event handler method name. Only instance methods on the generated or code-behind class are valid.
Is there a way to work around this? Does using custom events means avoiding commands and using the events aggragator?
Thank you,

Jan 21, 2009 at 7:37 PM

Hi Ken,


To bind a command to a control by XAML, the control must have a Command dependency property. For example, the Button class allows your bind a command to the Click event through its Command property:


<Button Name="MyCommandButton" Command="{Binding MyCommand}">Execute MyCommand</Button>


However, if your control does not contain a Command dependency property to bind a command with the AvailableItemSelected event, you could use instead an attached property with attached behaviors. This approach is being followed by the Prism-v2 team to hook buttons to commands via XAML in Silverlight (because they are not natively supported). Check the Click and ButtonBaseClickCommandBehavior classes inside the Command folder of the Composite.Presentation.Desktop project in the Prism V2 – Drop 9.


You can review the implementation of those classes to create similar classes (like AvailableItemSelected and ListSelectorAvailableItemSelectedCommandBehavior) to meet your requirements (instead of executing the command on Button’s Click event, you can execute it on SelectionChanged).

You could also find useful the following related article by Julian Dominguez explaining how to use Commands in Silverlight using attached behaviors:

·         ICommand for Silverlight with Attached Behaviors


Please let me know if this helps.


Mariano Converti

Jan 22, 2009 at 1:33 AM
Edited Jan 22, 2009 at 1:54 AM
Hi Mario,
Thank you for your answer. I understand now why my approach didn't work (Dah...).
Thank you,