views:

136

answers:

2

Hi, As topic says, I need to extend the features of a standard Silverlight ComboBox to also support Commanding. Since I follow MVVM I need my ComboBox to communicate the SelectionChanged event to my ViewModel.

What would the code look like for doing this? I want to be able to put the Command attribute on my ComboBox XAML control.

Using (WCF RIA, MVVM, VB.NET)..

All tips appricated!

+1  A: 

Create a behavior that exposes an ICommand Command and a object CommandParameter. In the behavior wire up to the SelectionChanged event of your AssociatedObject. Then you can bind the command to your behavior and simulate a command for the SelectionChanged event.

Stephan
+4  A: 

You can bind the property SelectedIndex or SelectedItem of the Combobox to your ViewModel. So you don´t need any Commands.

XAML

<ComboBox SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"/>

C#

public class ViewModel
{
   private int _selectedIndex;
   public int SelectedIndex {
     get { return _selectedIndex; }
     set { 
       if (value != _selectedIndex) {
         _selectedIndex = value;
         // Perform any logic, when the SelectedIndex changes.
       }
     } 
   }
}
Jehof
You can do the exact same thing with the SelectedItem property, if you also needed access to the object that is currently being selected. If for some reason you decide you want to command another event like Focus, I would check out http://www.galasoft.ch/mvvm/getstarted/#tutorials, look at the EventToCommand behavior section.
Agies
Great answer, I will go ahead and try
Mcad001