【问题标题】:IsHitTestVisible=false is disabling the ScrollBar of the DataGridIsHitTestVisible=false 正在禁用 DataGrid 的 ScrollBar
【发布时间】:2019-06-29 21:17:11
【问题描述】:

我想禁止选择DataGrid,因此我将属性IsHitTestVisible 设置为false,但随后它也会禁用ScrollBar

如何通过仍然禁用DataGrid 上的选择来启用滚动条?

我能做的一件事是:
我可以更改DataGrid 上的选择颜色。问题是当我说 Trigger Property="DataGridRow.IsSelected" 它不起作用。另一方面,如果我说TriggerProperty="DataGridCell.IsSelected" 就像这里说的Row Selection in DataGrid 一样,它只会选择第一列而不是整行。此外,如果我说背景的值是透明的,它不会在单元格中显示文本。请帮忙。

<DataGrid x:Name="DGRunInfoItems" IsHitTesVisible="False" IsReadOnly="True" ColumnWidth="*" FontSize="{StaticResource BRControlNormalFontSize}" ScrollViewer.VerticalScrollBarVisibility="Auto" HeadersVisibility="None" CanUserAddRows="False" ItemsSource="{Binding RunViewModel.RunInfoDataTable}" AutoGenerateColumns="False">
  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding Header}">
      <DataGridTextColumn.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
          <Style.Triggers>
            <Trigger Property="DataGridCell.IsSelected" Value="True">
              <Setter Property="Background" Value="Transparent"/>
            </Trigger>
          </Style.Triggers>
          <Style.Setters>
            <Setter Property="FontWeight" Value="Bold"/>
          </Style.Setters>
        </Style>
      </DataGridTextColumn.CellStyle>
    </DataGridTextColumn>
    <DataGridTextColumn Binding="{Binding Value}"/>
  </DataGrid.Columns>
  <DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
      <Setter Property="MinHeight" Value="28"/>
    </Style>
  </DataGrid.RowStyle>
</DataGrid>

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    这终于奏效了。我通过删除 IsHitTestVisible 的样式来实现它。如果它对这里的某人有帮助,那就是解决方案。我对整个 DataGrid 应用了一种样式。请参阅 DataGrid.CellStyle。 (我从另一个 StackOverflow 帖子中获得了帮助,但找不到指向它的链接。)

    <DataGrid x:Name="DGRunInfoItems"  IsReadOnly="True" ColumnWidth="*" FontSize="{StaticResource BRControlNormalFontSize}" ScrollViewer.VerticalScrollBarVisibility="Auto" HeadersVisibility="None" CanUserAddRows="False" ItemsSource="{Binding RunViewModel.RunInfoDataTable}" AutoGenerateColumns="False">
                        <DataGrid.CellStyle>
                            <Style TargetType="{x:Type DataGridCell}">
                                <Style.Triggers>
                                    <Trigger Property="DataGridCell.IsSelected" Value="True">
                                        <Setter Property="BorderBrush">
                                            <Setter.Value>
                                                <SolidColorBrush Color="Transparent"/>
                                            </Setter.Value>
                                        </Setter>
                                        <Setter Property="Foreground"
                                Value="{DynamicResource
                                       {x:Static SystemColors.ControlTextBrushKey}}"/>
                                        <Setter Property="Background">
                                            <Setter.Value>
                                                <SolidColorBrush Color="Transparent"/>
                                            </Setter.Value>
                                        </Setter>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </DataGrid.CellStyle>
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding Header}" FontWeight="Bold">
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding Value}"></DataGridTextColumn>
                        </DataGrid.Columns>
                        <DataGrid.RowStyle>
                            <Style TargetType="DataGridRow">
                                <Setter Property="MinHeight" Value="28"/>
                            </Style>
                        </DataGrid.RowStyle>
                    </DataGrid> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 2023-02-08
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      相关资源
      最近更新 更多