您的位置:首页 > 其它

WPF - MVVM - 如何将ComboBox的Selectchange事件binding到ViewModel

2014-05-07 12:48 399 查看
将所有的事件,属性,都映射到ViewModel中。好处多多,以后开发尽量用这种模式。

解决方法:

使用System.Windows.Interactivity.dll,添加该dll到项目引用

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"


ComboBox映射的代码:

<ComboBox VerticalAlignment="Center" HorizontalAlignment="Left" MinWidth="120" Margin="10,0,0,0"
x:Name="cboTest"
DisplayMemberPath="FamilyName"
SelectedValuePath="IdFamily"
IsReadOnly="True"
ItemsSource="{Binding ListFamily}"
SelectedValue="{Binding Path=ViewModel.SelectedFamilyNameID, Mode=TwoWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
Validation.ErrorTemplate="{x:Null}"
Visibility="{Binding ComboBoxVisibility}"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding FamilySelectionChangedCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: