2

我有一个 wpf 数据网格,我在其中通过 C# 添加所有列和样式。

我已应用 ColumnHeaderStyle 如下:

  1. 写二传手为:

    var fontSizeSetter = new Setter {Property = Control.FontSizeProperty, Value = Convert.ToDouble(font.Size)};

  2. 风格为:

            var headerStyle = new Style();
            headerStyle.Setters.Add(fontSetter);
            headerStyle.Setters.Add(fontSizeSetter);
            headerStyle.Setters.Add(fontStyleSetter);
            headerStyle.Setters.Add(fontWeightSetter);
    
  3. 将它应用到我的Datagrid'sColumnHeaderStyle 为:

view.DataGrid.ColumnHeaderStyle = headerStyle;

当前问题:现在,当我设置FontFamilyFontStyle时,它​​会被应用。但列标题文本大小保持不变。它没有得到更新。 在此处输入图像描述

整个方法:

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);
        }
4

3 回答 3

7

使用此代码调整 Datagrid 中列标题的字体:

<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
    <Setter Property="FontSize" Value="10"/>
</Style>

于 2013-05-06T06:00:44.737 回答
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;
于 2013-05-06T06:23:16.823 回答
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>
于 2013-06-17T09:01:36.550 回答