I have write this code to change the color of a cell in WPF mydatagrid, but as result i have all the row colored and i don't know why.
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>
Here the solution :
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.