Search code examples
wpfeventsmvvmattachedbehaviors

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>

Solution

  • You would use a data trigger to trigger an event on a different UI element such as "enable / disable, or visible /invisible"

    If you want the selected element to show the object data in other UI elements then you would use data binding and set the datacontext of the UI data display elements to be bound to the currently selected item in the combo box.