简体   繁体   English

WPF DataGrid创建cellstyle覆盖默认样式

[英]WPF DataGrid creating cellstyle overrides the default style

I have a WPF DataGrid with autogenerated columns and i needed a very specific style in the cells as you can see in the next image.我有一个带有自动生成列的 WPF DataGrid,我需要在单元格中使用非常特定的样式,如下图所示。

在此处输入图像描述

To do this i have created CellStyles depending on the column index to get the desired colors composition, like this for headers.为此,我根据列索引创建了 CellStyles,以获得所需的 colors 组合,就像标题一样。

datagrid.Columns[colIndex].HeaderStyle = new Style(typeof(DataGridColumnHeader));
datagrid.Columns[colIndex].HeaderStyle.Setters.Add(new Setter(Control.BackgroundProperty, (SolidColorBrush)new BrushConverter().ConvertFrom(colors[colorIndex])));
datagrid.Columns[colIndex].HeaderStyle.Setters.Add(new Setter(Control.BorderBrushProperty, (SolidColorBrush)new BrushConverter().ConvertFrom(colors[colorIndex])));
datagrid.Columns[colIndex].HeaderStyle.Setters.Add(new Setter(Control.FontWeightProperty, FontWeights.Bold));
datagrid.Columns[colIndex].HeaderStyle.Setters.Add(new Setter(Control.FontSizeProperty, 18.0));

like this for cells that require a specific color像这样对于需要特定颜色的单元格

datagrid.Columns[colIndex].CellStyle = new Style(typeof(DataGridCell));
datagrid.Columns[colIndex].CellStyle.Setters.Add(new Setter(Control.BackgroundProperty, (SolidColorBrush)new BrushConverter().ConvertFrom(colors[colorIndex])));
datagrid.Columns[colIndex].CellStyle.Setters.Add(new Setter(Control.BorderBrushProperty, (SolidColorBrush)new BrushConverter().ConvertFrom(colors[colorIndex])));
datagrid.Columns[colIndex].CellStyle.Setters.Add(new Setter(TextBlock.FontWeightProperty, FontWeights.Bold));
datagrid.Columns[colIndex].CellStyle.Setters.Add(new Setter(TextBlock.FontSizeProperty, 18.0));
datagrid.Columns[colIndex].IsReadOnly = true;

Everything is ok to this point, but, when i click on a row, the cells which style has been defined with "CellStyle = new Style" becomes all white, background and foreground, and i don't know how what i have to change to get the row data to be visible.到目前为止一切正常,但是,当我点击一行时,用“CellStyle = new Style”定义样式的单元格变成全白,背景和前景,我不知道我必须如何改变使行数据可见。

在此处输入图像描述

Thanks!谢谢!

Define custom colours for the HighlightBrushKey and HighlightTextBrushKey brushes:HighlightBrushKeyHighlightTextBrushKey画笔定义自定义颜色:

<DataGrid ...>
    <DataGrid.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
    </DataGrid.Resources>
    ...
</DataGrid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM