如何在滚动时阻止WPF数据网格自动选择行

时间:2022-06-08 20:26:05

below is my datagrid's definition:

下面是我的datagrid的定义:

<DataGrid
    Name="ODataGrid"
    AutoGenerateColumns="False"
    Grid.Row="0"
    Grid.Column="0"
    CanUserAddRows="False"
    AlternationCount="2"
    HeadersVisibility="All"
    IsReadOnly="True"                               
    SelectionUnit="FullRow"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    Background="#EBF0F0"
    AlternatingRowBackground="#E9E8EF"
    HorizontalGridLinesBrush="#7F7F7F"
    VerticalGridLinesBrush="#7F7F7F"
    SelectedItem="{Binding vmItem}"
    CellStyle="{StaticResource ODataGridCellStyle}"
    ItemsSource="{Binding vmResults}"
    Tag="{Binding DataContext, ElementName=BaseGrid}">

Below are the steps I took to get the error.

以下是我为获取错误而采取的步骤。

  1. The first row was selected by default.
  2. 默认选择第一行。
  3. I select another row in the datagrid.
  4. 我在datagrid中选择另一行。
  5. I do a sort by clicking on one of the column header.
  6. 我点击其中一个列标题进行排序。
  7. I do a scroll by dragging the vertical scroll bar.
  8. 我通过拖动垂直滚动条来滚动。
  9. The initial first row was somehow auto selected.
  10. 最初的第一行以某种方式自动选择。
  11. So now, there are 2 rows being selected.
  12. 所以现在,有两行被选中。

Why this behaviour? Anyway to prevent the auto select on scroll? Thanks.

为什么会这样?无论如何要阻止自动选择滚动?谢谢。

2 个解决方案

#1


0  

EnableRowVirtualization="False"

In the datagrid properties.

在datagrid属性中。

#2


-2  

add this property in datagrid

在datagrid中添加此属性

VirtualizingStackPanel.IsVirtualizing="False"

#1


0  

EnableRowVirtualization="False"

In the datagrid properties.

在datagrid属性中。

#2


-2  

add this property in datagrid

在datagrid中添加此属性

VirtualizingStackPanel.IsVirtualizing="False"