2013-01-31 36 views
2

我有一个组合框:实施中的ViewModel事件处理器与MVVM WPF

<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}"/> 

我试图让LostFocus事件处理程序移动到视图模型,因为我做了一些错误的引领者的SelectedValue绑定内处理“的ConnectionString “在ViewModel中找到。我希望这发生在用户重新选择相同的ComboBoxItem,这会触发OnPropertyChanged,除非选择了不同的列表项。

在错误

A“结合”以上结合导致不能在 类型“组合框”的“AddLostFocusHandler”属性被设置。 '绑定'只能在DependencyObject的DependencyProperty 上设置。

无论用户选择什么,我如何在ViewModel中选择ComboBox中的任何项目时触发可重复的代码?

回答

3

你需要包括对System.Windows.Interactivity DLL的引用,但它会是这个样子:

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> 
+1

+1包括ns –

0

乔什的回答有不同的命名空间为我工作:

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>