WPF Datagrid中的单个单元格背景颜色并非所有行颜色

问题描述 投票:0回答:1

我已编写此代码来更改WPF mydatagrid中单元格的颜色,但结果我将所有行都着色,我不知道为什么。

Style style = this.FindResource("backColor") as Style;
DataGridClientFile.CellStyle = style;


<Style x:Key="backColor" TargetType="DataGridCell">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Colore}" Value="Red">
                        <Setter Property="Background" Value="Red"/>
                        <Setter Property="Foreground" Value="White"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=Colore}" Value="Green">
                        <Setter Property="Background" Value="Green"/>
                        <Setter Property="Foreground" Value="White"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=Colore}" Value="Yellow">
                        <Setter Property="Background" Value="Yellow"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=Colore}" Value="Cyan">
                        <Setter Property="Background" Value="Cyan"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=Colore}" Value="White">
                        <Setter Property="Background" Value="White"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
c# wpf cell background-color wpfdatagrid
1个回答
0
投票

这里的解决方案:

Style style = this.FindResource("backColor") as Style;
Style styleNX = this.FindResource("backColorNX") as Style;
if (DataGridClientFile.Columns.Count >= 14)
            {
                DataGridColumn col = DataGridClientFile.Columns[14];
                col.CellStyle = style;
                DataGridColumn colNX = DataGridClientFile.Columns[15];
                colNX.CellStyle = styleNX;
            }
© www.soinside.com 2019 - 2024. All rights reserved.