WPF Datagrid格式化一个基于其他单元格的单元格

时间:2015-07-02 09:10:23

标签: c# wpf datagrid wpfdatagrid

我对WPF Datagrid有一个小问题。我的数据网格项源在名为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>

1 个答案:

答案 0 :(得分:0)

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

<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操作来操作静态资源。