【问题标题】:How to: Change the DataGrid IsSelected Background (C#, WPF)如何:更改 DataGrid IsSelected 背景(C#、WPF)
【发布时间】:2014-05-16 13:13:14
【问题描述】:

如何:更改 DataGrid IsSelected 背景(C#、WPF)

我的App.xaml 中有以下代码:

<Application.Resources>
    <Style TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Orange" />
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="WhiteSmoke" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Application.Resources>

不幸的是,我得到以下结果:

我需要做什么来解决我的问题?

【问题讨论】:

    标签: c# wpf xaml datagrid background


    【解决方案1】:

    您可以将这部分 Xaml 添加到您的数据网格资源中...

                    <DataGrid.Resources>
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow"/>
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Red"/>
                    </DataGrid.Resources>
    

    两个刷子一起工作,以在选择行时设置背景/前景。

    要为 DataGrid 行设置默认颜色,可以添加这个 sn-p...

                    <DataGrid.RowStyle>
                        <Style TargetType="DataGridRow">
                            <Setter Property="Background" Value="Pink" />
                            <Setter Property="Foreground" Value="DodgerBlue"/>
                        </Style>
                    </DataGrid.RowStyle>
    

    这会将背景设置为粉红色,将前景设置为 DodgerBlue。

    有关 SystemColors 静态资源的更多信息位于 http://blogs.msdn.com/b/wpf/archive/2010/11/30/systemcolors-reference.aspx

    【讨论】:

    • 好的,谢谢。第一个工作 - 第二个不工作。但是,我决定使用方法一。不幸的是,我现在需要更改前景。我该怎么做?
    • @Exception 为 SystemColors.ControlTextBrush 添加一个条目并检查您的结果
    • @Exception,为样式添加了默认属性,这可能会解决第二个不起作用的原因...
    • 好的,我试图解决我的问题,但是您发送了鼠标悬停效果和一般背景的代码。我只需要IsSelected 属性...
    • @Exception,刚查了一下,你有没有看到关于 SystemColors.ControlTextBrush 的评论?
    【解决方案2】:

    您必须在DataGridCell 上设置IsSelected 属性的样式,因为它(显然)在DataGridRow 之后呈现。

    【讨论】:

    • 好的,但现在我看不到我的文字了。它被WhiteSmoke 背景覆盖。
    • @Exception 然后在IsSelected 触发器中添加新的setter 以更改您选择的单元格Foreground 颜色。
    猜你喜欢
    • 2020-08-05
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 2015-05-14
    • 2013-01-27
    • 1970-01-01
    • 2018-09-26
    • 2016-11-15
    相关资源
    最近更新 更多