ComboBoxItem 的 WPF ICommand 实现

WPF ICommand Implementation for ComboBoxItem

如何在 WPF(4.5) ComboBoxItem 中实现 ICommand?我尝试了 CommandBinding,但收到错误消息 "Command' property of type 'CommandBinding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject"

  1. 添加对System.Windows.Interactivity的引用,如下所示
  2. 然后在 Windows.xaml 中添加下面一行。这将添加对 XAML

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

并且您的 XAML 将如下所示

你的 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>