0

I have a DataGrid as below

<DataGrid Margin="10,89,10,10" 
          AlternatingRowBackground="#FFB9E2FF" 
          ItemsSource="{Binding ResultDetails, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
          SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
          AutoGenerateColumns="True" 
          CanUserAddRows="False" 
          IsReadOnly="True" 
          SelectionMode="Single">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseRightButtonUp">
            <cmd:EventToCommand Command="{Binding ResultGridMouseClickCommand, Mode=OneWay}" />
        </i:EventTrigger>
        <i:EventTrigger EventName="MouseDoubleClick">
            <cmd:EventToCommand Command="{Binding ResultGridDblClickCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</DataGrid>

And I have a RelayCommand to handle MouseRightButtonUp event

private void ExecResultGridMouseClickCommand()
{
    if (SelectedItem == null) return;

    DisplayInfo(SelectedItem);
}

Let's say I have 5 items in DataGrid: Item1 Item2 Item3 Item4 Item5

When DataGrid init, SelectedItem is null. Then user right click on Item3, RelayCommand fire, but SelectedItem still is null. User right click on Item1, RelayCommand fire again, however, SelectedItem return Item3, and my code was display wrong item's details.

Is it possible to retrieve which item are selected in MouseRightButtonUp event? Or I should use other event instead?

Prisoner
  • 1,839
  • 2
  • 22
  • 38
  • Why don't you apply you logic after SelectedItem Change instead of using trigger. – Nikhil Agrawal Jun 16 '14 at 05:07
  • @NikhilAgrawal I want to display information when user right click only. Setter of `SelectedItem` will be triggered by double click and click. For this case, I may need a flag in right click event, and display info in setter of `SelectedItem` when flag on, then reset the flag, but I think this not a good approach, or this is a best practice? – Prisoner Jun 16 '14 at 05:49
  • Another option I found is use `VisualTreeHelper` in `MouseButtonEventArgs` to locate the selected row, I haven't try this yet, because I want to find some solution without UI element. for other people reference: http://www.scottlogic.com/blog/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row.html – Prisoner Jun 16 '14 at 06:08
  • Updated: for my first comment about `SelectedItem`, I found 1 more problem, which after display the information, user are able to right click the item to display the info again, however, `SelectionChanged` event or `SelectedItem` will not be triggered when select the same item. – Prisoner Jun 16 '14 at 06:35

1 Answers1

0

IMHO, the best approach is to apply a style to the DataGridRow, that way you ensure that the command is being applied to the item the user is actually clicking and you forget about the SelectedItem. Note the following:

  • DataContext will now the item itself instead of the collection.
  • You cannot use <i:Interaction.Triggers> inside a Style. There seem to be workarounds to this. Or you can try switching to the mighty Attached Command Behaviors: you have an example of usage inside a style here, second answer.
Community
  • 1
  • 1
Natxo
  • 2,917
  • 1
  • 24
  • 36
  • @Naxto Thanks for the ans. At the end, I have change the `DataGrid` with hover-select enable (inspired by your ans), it will assign `SelectedItem` before right click, drawback is, it will assign even control is disable... – Prisoner Jun 19 '14 at 03:59