0

I have a ContextMenu on a DataGrid and I want to mark a MenuItem as checked or unchecked depending on the item in the grid which is right-clicked.

So, I bind the 'IsChecked' property on the MenuItem to a property on my ViewModel, and this property is set to true or false by my VM according to item that is right-clicked.

However, turns out that 'IsChecked' property of my ContextMenu Item is evaluated only once. It is not evaluated everytime I rightclick an item.

For all subsequent right-clicks, the evaluation carried out the first time is retained.

The getter for property 'IsCheckedonVM' is not fired.

<MenuItem Command= IsCheckable="True"
                   IsChecked="{Binding IsCheckedonVM}"
                   Header = ".."
    </MenuItem>

in the VM:

public bool IsCheckedonVM
{
get
{ 
  return selectedItem.IsChecked;
}
set
{
  selecteditem.IsChecked = value;
  OnPropertyChanged("IsCheckedonVM");
}

How can I get the IsChecked property on my MenuItem to be evaluated everytime its rightclicked, so that IsCheckedonVM is fetched everytime?

Omri Btian
  • 6,499
  • 4
  • 39
  • 65
  • Try binding a command to your DataGrid`s right click event and executed dedicated logic to determine `IsCheckedonVM` value based on the `SelectedItem` of the grid ... – Omri Btian Oct 14 '13 at 09:35

1 Answers1

0

You can try binding a command to your DataGrids right click event and executed dedicated logic to determine IsCheckedonVM value based on the SelectedItem of the grid

Example (using Prism framework) :

    <DataGrid x:Name="myGrid" ItemsSource="{Binding SomeCollection}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseRightButtonDown">
                <i:InvokeCommandAction Command="{Binding DetermineIsCheckedCommand}" CommandParameter="{Binding ElementName=myGrid, Path=SelectedItem}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </DataGrid>

And in command:

DetermineIsCheckedCommand = new DelegateCommand<object>(selectedItem =>
{
    // Do logic and set IsCheckedonVM accordingly
});
Omri Btian
  • 6,499
  • 4
  • 39
  • 65
  • I actually want to show in the grid'scontext menu whether the property is checked or unchecked for the right-clicked item.Can't do away with the context menu option. – user2878253 Oct 14 '13 at 10:15