【问题标题】:Change cell/row text colour when cell/row is selected - DataGrid WPF选择单元格/行时更改单元格/行文本颜色 - DataGrid WPF
【发布时间】:2017-08-07 11:45:43
【问题描述】:

如何在选择 DataGrid Row 时以及当它处于非活动选择模式时(它被选中并且现在用户单击另一个控件,即文本框)时,我的行中的文本保持颜色,例如白色。

我试过这个(设置单元格样式):

<DataGrid.CellStyle>
    <StaticResource ResourceKey="DataGridCentering"/>
</DataGrid.CellStyle>

我在 App.Xaml 中所说的:

<Style x:Key="DataGridCentering" TargetType="{x:Type DataGridCell}">
     <Setter Property="Template">
         <Setter.Value>
             <ControlTemplate TargetType="{x:Type DataGridCell}">
                 <Grid Background="{TemplateBinding Background}">
                     <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"  />
                 </Grid>
              </ControlTemplate>
         </Setter.Value>
     </Setter>
     <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
     <Style.Triggers>
         <Trigger Property="IsSelected" Value="True">
             <Setter Property="Foreground" Value="White"/>
         </Trigger>
      </Style.Triggers>
</Style>

因为可以注意到,我尝试用触发器进行,即,当Cell选择颜色时,我的文本用白色颜色等, 但不知不觉这是行不通的

My text in DataGrid when cell/row is selected is still black..

【问题讨论】:

标签: c# wpf colors cell wpfdatagrid


【解决方案1】:

试试这种风格

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Style.Triggers>
            <Trigger Property="IsSelected"
                     Value="True">
                <Setter Property="Foreground"
                        Value="Red" />
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGrid.Resources>

【讨论】:

    【解决方案2】:

    尝试将以下画笔资源添加到DataGrid

    <DataGrid>
        <DataGrid.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White"/>
        </DataGrid.Resources>
        ...
    </DataGrid>
    

    应该在 Windows 7 上工作。在 Windows 8 及更高版本上,您必须覆盖 DataGridRow 的控制模板。

    【讨论】:

    • 比现在覆盖它可能更好,以防它在其他操作系统上使用......我真的想知道为什么触发器在这里不起作用,我认为它们必须起作用100%!
    猜你喜欢
    • 1970-01-01
    • 2018-10-03
    • 2023-03-28
    • 1970-01-01
    • 2015-12-16
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多