-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"

fynl
  • 38
  • 2
  • Please post your code - what property on ComboBoxItem are you trying to bind to ? – auburg Oct 03 '17 at 08:15
  • 1
    Possible duplicate of [WPF ICommand MVVM implementation](https://stackoverflow.com/questions/1468791/wpf-icommand-mvvm-implementation) – rmjoia Oct 03 '17 at 08:22
  • Command is used to handle user input from buttons (mostly) If you want to have something(?) execute when selecting a item in a `ComboBox` I suggest either fire an event or use databinding. – Bojje Oct 03 '17 at 08:34
  • Hi rmjoia, this is not a duplicate of the link you have posted. Please do not infer. – fynl Oct 03 '17 at 08:45
  • Hi Bojie, I am suppose to strictly follow MVVM. – fynl Oct 03 '17 at 08:48
  • @ReizTroyDurante You should edit your question to include relevant code, not throw it somewhere in comments. – svgrafov Oct 03 '17 at 09:08
  • go for interactions – Ramankingdom Oct 03 '17 at 11:57
  • Thanks everyone! 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. – fynl Oct 05 '17 at 02:54

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,478
  • 2
  • 12
  • 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"} – fynl 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. – fynl Oct 05 '17 at 02:52
  • can you please mark as answer. – Ramankingdom Oct 05 '17 at 05:33