WPF application uses DataGrid with touch screen how to make multiple selection?

Jane Jie Chen 506 Reputation points
2021-10-12T17:34:33.467+00:00

we have a Samples list view which needs to freeze the first three columns.
reference the link: https://learn.microsoft.com/en-us/answers/questions/548781/wpf-listviewgridview-is-it-possible-to-fix-first-s.html

the solution is "to use DataGrid, which is better than ListView. You can use DataGrid to make multiple selections with Ctrl+Click, because its SelectionMode property is set to SelectionMode="Extended" by default. DataGrid also supports sorting by clicking on the column headers. You could refer to the document and sort according to your requirements."

Our WPF application runs on touch screen.
WPF DataGrid behavior: click a row, it unselects previous selected row(s), select the clicked row.
To select multiple rows, press Ctrl key and click each row you want to select.

However, in touch screen, we cannot click ctrl key and the same time to click on each row.
how to make multiple row selections in DataGrid with touch screen? Thx!

Developer technologies | Windows Presentation Foundation
{count} votes

Answer accepted by question author
  1. Jane Jie Chen 506 Reputation points
    2021-11-03T07:29:01.427+00:00

    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:

    1. 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.

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Jane Jie Chen 506 Reputation points
    2021-11-04T15:39:41.477+00:00

    Hi HuiLiu,

    thanks for your helps and the problem has been solved.
    without press Ctrl or Shift key, we can click left mouse and right mouse on DataGrid rows to select/unselect multiple rows.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.