Command misunderstanding

Topics: Prism v2 - Silverlight 2
Feb 17, 2009 at 8:52 AM

Why we could invoke commands only by click? What if I want to invoke command after combox box selection changed?

Alexey Zakharov
Feb 17, 2009 at 3:11 PM

Hi Alexey,


You could implement your own attached properties with attached behaviors to invoke commands in other controls and events. 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) and emulates how commands work natively in WPF. Check the Click and ButtonBaseClickCommandBehavior classes inside the Command folder of the Composite.Presentation.Silverlight project in the Prism V2 – Drop 10.


You can review the implementation of those classes to create similar classes (like SelectionChanged and ComboBoxSelectionChangedCommandBehavior) to meet your requirements and be able to execute a command on the ComboBox’s SelectionChanged event.


For example, the StockTrader Reference Implementation reuses this infrastructure to execute a command when the Enter key is pressed on the Add Watch TextBox, so it does not rely on the Button’s Click event.


In the case of the ComboBox, there is also an easier way if you want to avoid using commands. You can have a property in your ViewModel, and in the view, you use TwoWay binding to notify the ViewModel of the selection change.

<ComboBox ItemsSource="{Binding Customers}" SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}" />


You can read more about commands in the following topic of the Prism v2 documentation:

·         Using Commands in Silverlight section of the topic Technical Concepts | Commands.

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

Feb 17, 2009 at 4:45 PM

Hi Mariano,

I have a situation where we are using the Office Ribbon control in our composite WPF app.  With this control as it is today, I cannot invoke a Global Command via XAML. 


What would be the recommended way to invoke a Global Command in the code behind file on the click event method?


Looking at the Prism V2 – Drop10  documentation all I see are examples that wire up the commands via XAML.  



Scott Nichols