首页 > 其他 > 详细

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

时间:2014-05-07 14:50:24      阅读:578      评论:0      收藏:0      [点我收藏+]

将所有的事件,属性,都映射到ViewModel中。好处多多,以后开发尽量用这种模式。

解决方法:

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

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

ComboBox映射的代码:

bubuko.com,布布扣
<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>
bubuko.com,布布扣

 

WPF - MVVM - 如何将ComboBox的Selectchange事件binding到ViewModel,布布扣,bubuko.com

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

原文:http://www.cnblogs.com/mantian/p/3713524.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!