我有一个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 它会被应用。但列标题 textsize 保持不变。它没有得到更新。
整个方法:
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);
}
答案 0 :(得分:7)
使用此代码调整Datagrid中列标题的字体:
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="FontSize" Value="10"/>
</Style>
答案 1 :(得分:0)
您可以尝试以下代码。
var headerStyle = new Style();
Setter fontSetter = new Setter { Property = Control.FontFamilyProperty, Value = new FontFamily("Calibri") };
headerStyle.Setters.Add(fontSetter);
Setter fontSizeSetter = new Setter { Property = Control.FontSizeProperty, Value = Convert.ToDouble(20) };
headerStyle.Setters.Add(fontSizeSetter);
Setter fontStyleSetter = new Setter { Property = Control.FontStyleProperty, Value = FontStyles.Italic };
headerStyle.Setters.Add(fontStyleSetter);
Setter fontWeightSetter = new Setter { Property = Control.FontWeightProperty, Value = FontWeights.Bold };
headerStyle.Setters.Add(fontWeightSetter);
myGrid.ColumnHeaderStyle = headerStyle;
答案 2 :(得分:0)
我在我的数据网格资源中编写了以下数据模板。它有效。
<DataTemplate DataType="{x:Type System:String}">
<TextBlock Text="{Binding}">
<TextBlock.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGridColumnHeader}, Path=FontFamily}" />
<Setter Property="FontSize" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGridColumnHeader}, Path=FontSize}" />
<Setter Property="FontStyle" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGridColumnHeader}, Path=FontStyle}"/>
<Setter Property="FontWeight" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGridColumnHeader}, Path=FontWeight}"/>
</Style>
</TextBlock.Resources>
</TextBlock>
</DataTemplate>