-3

How can I implement ICommand in WPF(4.5) ComboBoxItem? I tried CommandBinding but I getting error message "Command' property of type 'CommandBinding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject"

1 Answers1

-1
  1. Add Reference to System.Windows.Interactivity as shown below enter image description here
  2. Then in Windows.xaml add below line . This will add reference to XAML

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

and your XAML will be like as given below enter image description here

And your Combobox XAML be like : -

<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>
Ramankingdom
  • 1,468
  • 2
  • 11
  • 17
  • I got error: {"Could not load file or assembly 'System.Windows.Interactivity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"System.Windows.Interactivity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"} – Reiz Troy Durante Oct 04 '17 at 00:11
  • You have to add as reference – Ramankingdom Oct 04 '17 at 06:48
  • @ReizTroyDurante I added the code reference – Ramankingdom Oct 04 '17 at 07:06
  • Thanks Ramankingdom! I already found the root cause. The reason for the error is Ive referenced a wrong version of Sytem.Windos.Interactivity(4.5) which is incompatible with the MahApps(4.0) version I am using. – Reiz Troy Durante Oct 05 '17 at 02:52
  • can you please mark as answer. – Ramankingdom Oct 05 '17 at 05:33