WPF数据网格基于另一个单元格的连接来格式化一个单元格

本文关键字:单元格 格式化 一个 连接 数据网 数据 网格 另一个 WPF | 更新日期: 2023-09-27 18:00:51

我对WPF数据网格有一个小问题。我的数据网格项源于一个名为Case的SQL表,但在代码中有几个属性添加到Case中。其中之一是ColorForTAT,它包含用于着色情况的逻辑,但是该属性对于用户来说不应该是可见的
直到现在,我设法让它为整行设置背景色,但我被要求只为一列上色(Name="TimeTillTatExternal"(。我试着用谷歌搜索它,但找不到任何工作。

        <DataGrid Name="DataGridCase" AutoGenerateColumns="False" IsReadOnly="True" Grid.Row="9" Grid.RowSpan="3"  Grid.ColumnSpan="10" VerticalAlignment="Top" Margin="10,0" SelectionChanged="DataGridCase_SelectionChanged">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="CaseNumber" Binding="{Binding CaseNumber }" Header="Case Number"/>
            <DataGridTextColumn x:Name="Status" Binding="{Binding FinalStatus }" Header="Tool status"/>
            <DataGridTextColumn x:Name="Title" Binding="{Binding Title }" Header="Title"/>
            <DataGridTextColumn x:Name="Country" Binding="{Binding CountryFromAssignemnt }" Header="Country"/>
            <DataGridTextColumn x:Name="CreatedDate" Binding="{Binding CreatedDate, StringFormat = 'dd/MM/yyyy HH:mm' }" Header="Created date"/>
            <DataGridTextColumn x:Name="TATExternal" Binding="{Binding TATExternal, StringFormat = 'dd/MM/yyyy HH:mm' }" Header="Due date External"/>
            <DataGridTextColumn x:Name="TATInternal" Binding="{Binding TATInternal, StringFormat = 'dd/MM/yyyy HH:mm'  }" Header="Due date Internal"/>
            <DataGridTextColumn x:Name="Assignment" Binding="{Binding Assignment }" Header="Assignment"/>
            <DataGridTextColumn x:Name="TimeTillTatExternal" Binding="{Binding TimeTillTatExternal }" Header="Days left"/>
            <DataGridTextColumn x:Name="Status3" Binding="{Binding LinkedIncident }" Header="LinkedIncident "/>
            <DataGridTextColumn x:Name="Status4" Binding="{Binding ExternalSupport}" Header="ExternalSupport"/>
            <DataGridTextColumn x:Name="Status2" Binding="{Binding SDStatus }" Header="Service Desk Status"/>
        </DataGrid.Columns>
        <DataGrid.ItemContainerStyle>
            <Style TargetType="{x:Type DataGridRow}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ColorForTAT }" Value="black">
                        <Setter Property="Background" Value="Gray"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ColorForTAT }" Value="gray">
                        <Setter Property="Background" Value="LightGray"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ColorForTAT }" Value="red">
                        <Setter Property="Background" Value="Red"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ColorForTAT }" Value="yellow">
                        <Setter Property="Background" Value="Yellow"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ColorForTAT }" Value="green">
                        <Setter Property="Background" Value="Green"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.ItemContainerStyle>
    </DataGrid>

WPF数据网格基于另一个单元格的连接来格式化一个单元格

您需要设置每一列CellStyle,并将Background绑定到可以在Window中声明的静态资源。参考资料:示例代码如下所示,请根据您的要求更改代码。

<Window x:Class="WpfApplication1.MainWindow" ...>
<Window.Resources>
    <SolidColorBrush x:Key="clBr" Color="White" />
</Window.Resources>

列:

  <DataGridTextColumn Header="First Name" 
                                Binding="{Binding Path=FirstName}">
            <DataGridTextColumn.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="Background" 
                            Value="{StaticResource clBr}" />
                </Style>
            </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>

然后您可以通过代码或xaml操作来操作静态资源。