我有一个 wpf 数据网格,我在其中通过 C# 添加所有列和样式。
我已应用 ColumnHeaderStyle 如下:
写二传手为:
var fontSizeSetter = new Setter {Property = Control.FontSizeProperty, Value = Convert.ToDouble(font.Size)};
风格为:
var headerStyle = new Style(); headerStyle.Setters.Add(fontSetter); headerStyle.Setters.Add(fontSizeSetter); headerStyle.Setters.Add(fontStyleSetter); headerStyle.Setters.Add(fontWeightSetter);
将它应用到我的
Datagrid's
ColumnHeaderStyle 为:
view.DataGrid.ColumnHeaderStyle = headerStyle;
当前问题:现在,当我设置FontFamily和FontStyle时,它会被应用。但列标题文本大小保持不变。它没有得到更新。
整个方法:
private static void ConfigureFontsForDataGrid(Views.StatusMonitor view, StatusMonitorAgencyFontType font)
{
var fontfamily = new FontFamily(font.Font);
var fontSetter = new Setter { Property = Control.FontFamilyProperty, Value = fontfamily };
var fontSizeSetter = new Setter { Property = Control.FontSizeProperty, Value = Convert.ToDouble(font.Size) };
var fontStyleSetter = new Setter { Property = Control.FontStyleProperty };
var fontWeightSetter = new Setter { Property = Control.FontWeightProperty };
// Defaults
fontWeightSetter.Value = FontWeights.Regular;
fontStyleSetter.Value = FontStyles.Normal;
switch (font.Style)
{
case "Regular":
fontWeightSetter.Value = FontWeights.Regular;
fontStyleSetter.Value = FontStyles.Normal;
break;
case "Bold Italic":
fontWeightSetter.Value = FontWeights.Bold;
fontStyleSetter.Value = FontStyles.Italic;
break;
}
//Configuring data grid cell font
view.DataGrid.CellStyle.Setters.Add(fontSetter);
view.DataGrid.CellStyle.Setters.Add(fontSizeSetter);
view.DataGrid.CellStyle.Setters.Add(fontStyleSetter);
view.DataGrid.CellStyle.Setters.Add(fontWeightSetter);
//Configuring data grid column header font
view.DataGrid.ColumnHeaderStyle.Setters.Add(fontSetter);
view.DataGrid.ColumnHeaderStyle.Setters.Add(fontSizeSetter);
view.DataGrid.ColumnHeaderStyle.Setters.Add(fontStyleSetter);
view.DataGrid.ColumnHeaderStyle.Setters.Add(fontWeightSetter);
}