Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何:更改DataGrid IsSelected后台(C#,WPF)_C#_Wpf_Xaml_Datagrid_Background - Fatal编程技术网

如何:更改DataGrid IsSelected后台(C#,WPF)

如何:更改DataGrid IsSelected后台(C#,WPF),c#,wpf,xaml,datagrid,background,C#,Wpf,Xaml,Datagrid,Background,如何:更改DataGrid IsSelected后台(C#,WPF) 我的App.xaml中确实有以下代码: <Application.Resources> <Style TargetType="{x:Type DataGridRow}"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> &l

如何:更改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>

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


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

您必须设置
DataGridCell
上的
IsSelected
属性的样式,因为它(显然)是在
DataGridRow
之后呈现的。您可以将这一位Xaml添加到数据网格资源中

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

当选择一行时,两个笔刷一起工作以设置背景/前景

要设置DataGrid行的默认颜色,可以添加以下代码段

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

这将背景设置为粉红色,前景设置为淡蓝色


有关SystemColor静态资源的更多信息位于

可以,但现在我无法再看到我的文本。它被
白烟
背景覆盖。@异常然后在
IsSelected
触发器中添加新的setter,以更改您选择的单元格
前景
颜色。好的,谢谢。第一个有效,第二个无效。然而,我决定使用方法一。不幸的是,我现在需要改变前景。我该怎么做?@Exception为SystemColor.ControlTextBrush添加一个条目并检查您的result@Exception,在样式中添加了默认属性,可以修复第二个不起作用的原因…好吧,我试图修复我的问题,但您发送了鼠标悬停效果和一般背景的代码。我只需要
IsSelected
属性…@异常,只是检查一下,你看到关于SystemColor.ControlTextBrush的评论了吗?