Amit
Amit

Reputation: 623

How to bind a SelectionChanged event of ComboBox in silverlight 4 using MVVM?

I am trying to bind the SelectionChanged event of ComboBox using RoutingCommand(same as we bind the command for Buttons). But not able to achieve it. I dnt like to use any third party control or to write the code in the property.

Upvotes: 0

Views: 2823

Answers (2)

Zabavsky
Zabavsky

Reputation: 13640

You can use System.Windows.Interactivity dll.

The code will be something like that:

<UserControl
 ...
 xmlns:ei="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
 ...>    
 ...
 <Combobox>
    <ei:Interaction.Triggers>
       <ei:EventTrigger EventName="SelectionChanged">
           <ei:InvokeCommandAction Command="{Binding Command}"/>
       </ei:EventTrigger>
    </ei:Interaction.Triggers>
 </Combobox>
 ...    
</UserControl>

Upvotes: 1

dmytro.s
dmytro.s

Reputation: 720

Are you sure, that you need SelectionChanged event? Just bind

SelectedItem="{Binding Path=ComboSelected, Mode=TwoWay}"

Where ComboSelected is property of your ViewModel. And do in setter of ComboSelected property what you need.

Upvotes: 1

Related Questions