简体   繁体   English

c#:Datagrid:通过 DataTriggers 更改选择的前景色

[英]c#: Datagrid: Changing Foreground Color of Selection by DataTriggers

I want to costumize the colors of a Datagrid.我想定制 Datagrid 的 colors。

My code is as follows我的代码如下

<Window x:Class="DataGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DataGridTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <DataGrid Grid.Row="1"
                  VerticalAlignment="Stretch" 
                  VerticalContentAlignment="Top"
                  GridLinesVisibility="Horizontal" 
                  VerticalScrollBarVisibility="Visible"
                  AutoGenerateColumns="True"
                  SelectionUnit="FullRow"
                      >

            <DataGrid.RowStyle>

                <Style TargetType="DataGridRow">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=Automatic}" Value="True">
                            <Setter Property="FontStyle" Value="Italic"/>
                            <Setter Property="Foreground"  Value="Green" />
                            <Setter Property="Background" Value="GhostWhite"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=Automatic}" Value="False">
                            <Setter Property="Background" Value="FloralWhite"/>
                            <Setter Property="Foreground"  Value="Blue" />
                        </DataTrigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Foreground"  Value="Black" /> <!-- Why is this ignored -->
                            <Setter Property="Background"  Value="Red" />
                        </Trigger>
                    </Style.Triggers>
                </Style >
            </DataGrid.RowStyle>

            <DataGrid.Columns>
                <DataGridTextColumn Header="Subject" Binding="{Binding Path=Subject}"/>
                <DataGridTextColumn Header="Body" Binding="{Binding Path=Body}"/>
                <DataGridTextColumn Header="Automatic" Binding="{Binding Path=Automatic}"/>
            </DataGrid.Columns>

            <local:Dummy Subject="Subject 1" Body="Body 1" Automatic="True" />
            <local:Dummy Subject="Subject 2" Body="Body 2" Automatic="False"  />
            <local:Dummy Subject="Subject 3" Body="Body 2"  Automatic="False" />
        </DataGrid>
    </Grid>
</Window>

and the c#和 c#

using System.Windows;
namespace DataGridTest
{
    public class Dummy
    {
        public string Subject { get; set; }
        public string Body { get; set; }
        public bool Automatic { get; set; }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {      
            InitializeComponent();
        }
    }
}

The result looks like (Last Line is selected):结果看起来像(选择了最后一行):

在此处输入图像描述

What I was expecting : the whole last (selected) line in with black font on red background.我所期待的:整个最后(选定的)行在红色背景上带有黑色字体。

What I got: As you can see the font is now white and the background is partly red/blue .我得到了什么:如您所见,字体现在是白色的,背景部分是 red/blue

Interestingly in the first two lines the setter was defining the foreground colors as expected.有趣的是,在前两行中,setter 按预期定义了前景 colors。 Even If I deactivate the first two setters for the foreground, the selected text in the row still stays white.即使我停用前景的前两个设置器,行中选定的文本仍然保持白色。

Why is this the case?为什么会这样? How to define the desired coloring for the whole selection?如何为整个选择定义所需的颜色?

You should add handling of IsSelected for DataGridCell as well (in the same way with DataGridRow )您还应该为DataGridCell添加IsSelected的处理(与DataGridRow相同)

<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
         <Style.Triggers>
             <Trigger Property="IsSelected" Value="True">
                 <Setter Property="Foreground"  Value="Black" />
                 <Setter Property="Background"  Value="Red" />
             </Trigger>
          </Style.Triggers>
    </Style>
</DataGrid.CellStyle>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM