【问题标题】:c#: Datagrid: Changing Foreground Color of Selection by DataTriggersc#: Datagrid:通过 DataTriggers 更改选择的前景色
【发布时间】:2020-02-28 14:06:18
【问题描述】:

我想自定义 Datagrid 的颜色。

我的代码如下

<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>

和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();
        }
    }
}

结果看起来像(最后一行被选中):

我所期待的:整个最后(选定)行在 red 背景上使用 black 字体。

我得到了什么: 如您所见,字体现在是白色,背景是部分红色/蓝色

有趣的是,在前两行中,setter 按预期定义了前景色。即使我停用前景的前两个设置器,行中选定的文本仍然保持白色。

为什么会这样? 如何为整个选择定义所需的颜色?

【问题讨论】:

    标签: c# datagrid wpf-controls


    【解决方案1】:

    您还应该为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>
    

    【讨论】:

    • 谢谢帕维尔!快速而有用的答案。
    猜你喜欢
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 2016-05-22
    • 2015-04-12
    • 1970-01-01
    • 2013-01-30
    • 2013-08-21
    相关资源
    最近更新 更多