we just notice that right mouse click on a row does not work.
if all rows are selected, right mouse click on a row, it only selects that row and remove selections of other rows.
we add the following codes to make click both Left and Right mouse to work:
- add following line " || e.RightButton == MouseButtonState.Pressed". private void PreviewMouseDownHandler(object sender, MouseButtonEventArgs e)
{
if ((e.LeftButton == MouseButtonState.Pressed || e.RightButton == MouseButtonState.Pressed)
&& e.OriginalSource is FrameworkElement element
&& GetVisualParentOfType<DataGridRow>(element) is DataGridRow row)
{
row.IsSelected = !row.IsSelected;
e.Handled = true;
}
}
2) DataGridCell style to set IsHitTestVisible="False" in
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border x:Name="borderDataGrid"
Margin="-1"
BorderBrush="LightGray"
BorderThickness="1.5">
<Grid MinHeight="35" MaxHeight="40"
x:Name="gridCell"
VerticalAlignment="Center"
Background="{TemplateBinding Background}">
<!-- Disable right mouse click on grid cell-->
<ContentPresenter VerticalAlignment="Center"
**IsHitTestVisible="False"**/>
</Grid>
Note: since disable "IsHitTestVisible" in cell level and also in cell ContentPresenter. So it will not able to show context menu.
Disable in Cell ContentPresenter level, so right mouse click will not cause unselect other selected rows.