[英]WPF: Change DataGrid cell/row background color dynamically at runtime
[英]single cell background color in WPF Datagrid not all row color
我已經編寫了這段代碼來更改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>
這里的解決方案:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.