2
votes

I have a ComboBox:

<ComboBox x:Name="cbConnection"
                      ItemsSource="{Binding Source={StaticResource XmlConnectionList}, XPath=//ComboItem}"
                      DisplayMemberPath="Key"
                      SelectedValuePath="Value"
                      SelectedValue="{Binding Path=ConnectionString,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}" 
                      Margin="{StaticResource ConsistentMargins}"
                      Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}" Width="120"
                      LostFocus="{Binding Path=cbConnection_LostFocus}"/>

I am trying to get the LostFocus event handler moved to the ViewModel because I do some error handling within the setter for the SelectedValue binding "ConnectionString" found in the ViewModel. I want this to happen if a user reselects the same ComboBoxItem, which does fire OnPropertyChanged unless a different list item is selected.

The above binding results in error

A 'Binding' cannot be set on the 'AddLostFocusHandler' property of type 'ComboBox'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

How can I fire repeatable code within the ViewModel on selection of any item within a ComboBox, regardless of the user's selection?

2

2 Answers

3
votes

You'll need to include a reference to the System.Windows.Interactivity dll, but it will look something like this:

xmlns:b="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"  
<ComboBox>
<b:Interaction.Triggers>
    <b:EventTrigger EventName="LostFocus">
    <b:InvokeCommandAction  Command="{Binding cbConnection_LostFocus}" CommandParameter="{Binding}"/>
     </b:EventTrigger>
 </b:Interaction.Triggers>
</ComboBox>
0
votes

Josh's answer worked for me with a different namespace:

xmlns:b="http://schemas.microsoft.com/expression/2010/interactivity"  
<ComboBox>
<b:Interaction.Triggers>
    <b:EventTrigger EventName="LostFocus">
    <b:InvokeCommandAction  Command="{Binding cbConnection_LostFocus}" CommandParameter="{Binding}"/>
     </b:EventTrigger>
 </b:Interaction.Triggers>
</ComboBox>