WPF十四(DataGrid中列所在单元格进行整体变色)

背景:
DataGrid对应列中,根据值不同显示,不同的单元格背景色

解决方案:

 <DataGridTemplateColumn Width="*"  Header="Level" MaxWidth="150"  IsReadOnly="True" CanUserSort="True">
                                    <DataGridTemplateColumn.CellTemplate> 
                                        <DataTemplate>
                                            <Border x:Name="levelControl"  Margin="0,0,0,0"> 
                                                <TextBlock TextWrapping="Wrap" Text="{Binding Level}" ToolTip="{Binding Level}" Style="{StaticResource dgCell}" />
                                            </Border>
                                            <DataTemplate.Triggers>
                                                <DataTrigger Binding="{Binding Level}" Value="FATAL">
                                                    <Setter TargetName="levelControl"  Property="Background"   Value="DarkRed" ></Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding Level}" Value="ERROR">
                                                    <Setter TargetName="levelControl"  Property="Background"   Value="Red" ></Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding Level}" Value="EVENT">
                                                    <Setter TargetName="levelControl"  Property="Background"   Value="Green" ></Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding Level}" Value="OTHER">
                                                    <Setter TargetName="levelControl"  Property="Background"   Value="LightGreen" ></Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding Level}" Value="PROBLEM">
                                                    <Setter TargetName="levelControl"  Property="Background"   Value="LightGray" ></Setter>
                                                </DataTrigger>
                                            </DataTemplate.Triggers> 
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate> 
                                </DataGridTemplateColumn>

在TextBlock 外面加一个Border,即可进行解决。

你可能感兴趣的:(wpf,c#,经验分享)