ComboBoxItem的WPF ICommand实现

时间:2017-10-03 08:13:03

标签: wpf combobox icommand relaycommand

如何在WPF(4.5)ComboBoxItem中实现ICommand?我尝试过CommandBinding,但收到错误消息" Command'类型' CommandBinding'的属性。 A'绑定'只能在DependencyObject"

的DependencyProperty上设置

1 个答案:

答案 0 :(得分:-1)

  1. 添加对System.Windows.Interactivity的引用,如下所示 enter image description here
  2. 然后在Windows.xaml中添加以下行。这将添加对XAML的引用

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"   
    
  3. 您的XAML将如下所示 enter image description here

    你的Combobox XAML就像: -

    <ComboBox Name="myComboBox" SelectedValuePath="Content">
            <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding YourCommandName}" 
                 CommandParameter="{Binding ElementName=myComboBox, 
                  Path=SelectedValue}"/>
            </i:EventTrigger>
         </i:Interaction.Triggers>
         <ComboBoxItem Content="ItemOne" />
         <ComboBoxItem Content="ItemTwo" />
         <ComboBoxItem Content="ItemThree" />
    </ComboBox>