Commands and SelectionChanged

Topics: Prism v2 - Silverlight 2
Mar 11, 2009 at 8:00 PM

Using the Command.Click is pretty cool to wire up events to the ViewModel command.  

How should this be done for a SelectionChanged for a ComboBox for example?
Mar 11, 2009 at 10:29 PM
That is exactly what I was looking for.
It looks like we have to roll our own.
There is a section in the help guide under Technical Concepts / Commands / Extending Command Support
that describes how to do it.

Maybe someone will post some sample code for us.

Mar 11, 2009 at 10:32 PM
Yes, if you want to bind event X on control Y to a control, you will have to roll your own.

However, for your situation, there is a much easier solution. You can two way bind a property of your ViewModel to the SelectedItem of your Combobox.

Hope this helps.

Mar 12, 2009 at 4:03 PM
Edited Mar 12, 2009 at 4:09 PM
<Edit> Ok, I get it, I will tie into the setter of the public property on the ViewModel.

Erwin, interesting idea.

So if I Mode=TwoWay it, then what event would I tie into in my ViewModel.  When someone changes the comboobox, I need to run off to the DB and get some more data.  I am just now getting my head around MVVM and Prism, so it is not quite clear to me.

Mar 12, 2009 at 4:37 PM
The Erwin's idea works fine when the comboBox is located on the page and the ViewModel has a property for the SelectedItem of the ComboBox.

Where this does work is when the combo box is inside an ItemCollection datatemplate.

What I am finding the easiest thing to do is to have the Changed event declared in the code behind.

So the xaml looks like this

                            <Slider x:Name="InputSlider" 

Tag="{Binding Current}" 

Maximum="{Binding MaximumValue}" Minimum="{Binding MinimumValue}" Value="{Binding InputValue, Mode=TwoWay}" 

ValueChanged="InputSlider_ValueChanged" Width="180"></Slider>

The code behind

        private void InputSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)


            var fe = sender as FrameworkElement;

            this.ViewModel.OnSliderValueChanged(fe.Tag as IFuzzySet);


I am using the tag property to bind to the Current instance of the element being bound.  In ASP.Net this is the equivalent to DataItem property

This property is declared as  

IFuzzySet Current { get { return this; }}

Curently, I can live with that little bit of code behind the xaml page.  I follow patterns where they save me time and effort but this really is caused by a bit of short coming in the Commands only currently supporting the ButtonBase.

Hope this helps