Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you handle a ComboBox SelectionChanged in MVVM?

For those doing pure MVVM, how do you handle a ComboBox SelectionChanged event without reverting to code behind?

I tried e.g. AttachedBehaviors but Event="SelectedChanged" is not supported:

<ComboBox>     <ComboBoxItem Content="Test1">         <c:CommandBehaviorCollection.Behaviors>             <c:BehaviorBinding Event="SelectionChanged"                                 Command="{Binding SelectedChanged}"                                CommandParameter="MainBorder123"/>         </c:CommandBehaviorCollection.Behaviors>     </ComboBoxItem>     <ComboBoxItem Content="Test2"/>     <ComboBoxItem Content="Test3"/> </ComboBox> 
like image 780
Edward Tanguay Avatar asked Jun 04 '09 13:06

Edward Tanguay


People also ask

Which event can be used to detect changes in list combobox selection?

You can use "ComboBoxItem. PreviewMouseDown" event.

What is combo box WPF?

A combobox is a selection control that combines a non-editable textbox and a drop-down listbox that allows users to select an item from a list. It either displays the current selection or is empty if there is no selected item.


1 Answers

This post is quite old, but since I got the same issue. Here is how I solved it (using framework 4.0) : the idea is to use System.Windows.Interactivity.

In the XAML :

<ComboBox ItemsSource="{Binding Items}">     <i:Interaction.Triggers>         <i:EventTrigger EventName="SelectionChanged">             <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}"/>         </i:EventTrigger>     </i:Interaction.Triggers> </ComboBox> 

Then you just need to implement the SelectionChangedCommand in your viewmodel.

like image 60
fabien Avatar answered Sep 22 '22 09:09

fabien