【问题标题】:How to prevent WPF datagrid from auto select row upon scrolling如何防止 WPF 数据网格在滚动时自动选择行
【发布时间】:2014-09-30 06:48:21
【问题描述】:

以下是我的数据网格的定义:

<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}">

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

  1. 默认选择第一行。
  2. 我在数据网格中选择了另一行。
  3. 我通过单击其中一个列标题进行排序。
  4. 我通过拖动垂直滚动条进行滚动。
  5. 最初的第一行不知何故是自动选择的。
  6. 所以现在有 2 行被选中。

为什么会有这种行为?无论如何要防止滚动时自动选择?谢谢。

【问题讨论】:

  • 您是否检查过 DataGrid 没有设置 SelectionMode 属性 Set dg.SelectionMode = DataGridSelectionMode.Extended 将选择多于一行?
  • 嗨,我的 SelectionMode 必须是扩展的,因此我无法将其设置为 Single,尽管这样可以解决问题。

标签: wpf datagrid wpfdatagrid


【解决方案1】:
EnableRowVirtualization="False"

在数据网格属性中。

【讨论】:

    【解决方案2】:

    在数据网格中添加此属性

    VirtualizingStackPanel.IsVirtualizing="False"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      • 2010-09-17
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      相关资源
      最近更新 更多