简体   繁体   English

WPF DataGrid ColumnHeader样式:无法在ControlTemplate中使文本加粗

[英]WPF DataGrid ColumnHeader Style: Can't make text bold in ControlTemplate

I am using a DataGrid from the WpfToolkit. 我正在使用WpfToolkit中的DataGrid。 I have taken the Style resource dictionary for it and tweaked it a bit. 我已经为它准备了样式资源字典,并对其进行了一些调整。 What I am trying to accomplish is to make the header bold when a certain property on the data-bound object is True. 我要完成的工作是,当数据绑定对象的某个属性为True时,使标题变为粗体。 The Column Header is not necessarily a TextBlock and has its Control Template redefined as seen below: 列标题不一定是TextBlock,并且其控制模板已重新定义,如下所示:

DataGridStyle: DataGridStyle:

<Style x:Key="ModificationsDataGridStyle"  TargetType="{x:Type compCtrls:ModDataGrid}">
    <Setter Property="ColumnHeaderStyle" Value="{StaticResource DatagridColumnHeaderCustomTemplateStyle}" />
</Style>

ColumnHeaderStyle: ColumnHeaderStyle:

<Style x:Key="DatagridColumnHeaderCustomTemplateStyle" 
       TargetType="{x:Type primitives:DataGridColumnHeader}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="MinWidth" Value="0" />
    <Setter Property="MinHeight" Value="28" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FontWeight" Value="Normal" />
    <Setter Property="Cursor" Value="Hand" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type primitives:DataGridColumnHeader}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Border x:Name="BackgroundBorder" BorderThickness="0,1,0,1" 
                            Background="{StaticResource DataGridHeaderBackgroundBrush}" 
                            BorderBrush="{StaticResource DataGridHeaderBorderBrush}" 
                            Grid.ColumnSpan="2" />
                    <ContentPresenter x:Name="ContentPres" Margin="6,3,6,3" VerticalAlignment="Center" />
                    <Path x:Name="SortArrow" Visibility="Hidden" Data="M0,0 L1,0 0.5,1 z" Stretch="Fill" 
                          Grid.Column="1" Width="10" Height="7" Fill="White" Margin="0,0,7,0" 
                          VerticalAlignment="Center" RenderTransformOrigin="0.5,0.4" />
                    <Rectangle Width="1" Fill="#AAC377" HorizontalAlignment="Right" Grid.ColumnSpan="2" />

                    <Rectangle Width="1" Margin="0,0,1,0" Fill="#425B10" 
                               HorizontalAlignment="Right" Grid.ColumnSpan="2" />
                    <Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" 
                           Style="{StaticResource ColumnHeaderGripperStyle}"/>
                    <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" 
                           Style="{StaticResource ColumnHeaderGripperStyle}"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="SortDirection" Value="{x:Null}">
                        <Setter TargetName="SortArrow" Property="Visibility" Value="Collapsed" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" TargetName="BackgroundBorder" 
                                Value="{StaticResource DataGridHeaderMouseOverBackgroundBrush}" />
                        <Setter Property="BorderBrush" TargetName="BackgroundBorder" 
                                Value="{StaticResource DataGridHeaderBorderBrush}" />
                    </Trigger>
                    <Trigger Property="SortDirection" Value="Ascending">
                        <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                        <Setter TargetName="SortArrow" Property="Fill" Value="Goldenrod" />
                        <Setter TargetName="SortArrow" Property="RenderTransform">
                            <Setter.Value>
                                <RotateTransform Angle="180" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="SortDirection" Value="Descending">
                        <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                        <Setter TargetName="SortArrow" Property="Fill" Value="Brown" />
                    </Trigger>
                    <Trigger Property="DisplayIndex" Value="0">
                        <Setter Property="Visibility" Value="Collapsed" 
                                TargetName="PART_LeftHeaderGripper"></Setter>
                    </Trigger>
                    <DataTrigger Binding="{Binding IsRevisedSummableField}" Value="True">
                        <Setter TargetName="ContentPres" Property="Control.FontWeight" Value="Bold" />
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I thought using a DataTrigger (bound to the given property) in the template would work. 我认为在模板中使用DataTrigger(绑定到给定的属性)会起作用。 Either I am wrong or have not gone the right path in implementing this. 我错了,或者没有正确地实现这一目标。 I should also mention that the columns are Auto-Generated and once the columns are generated, I set the datacontext of each DataGridColumn as such: 我还应该提到,列是自动生成的,并且一旦生成列,就将每个DataGridColumn的datacontext设置为:

    protected override void OnAutoGeneratedColumns(EventArgs e)
    {
        var dataTable = pivotMod.DataTable;
        foreach (DataGridColumn gridCol in Columns)
        {
            var colName = gridCol.Header.ToString();
            DataColumn col = dataTable.Columns[colName];

            // give the column headers a pretty name
            gridCol.Header = col.Caption;

            // set the datacontext of the gridcolumn to the modfield ...
            ModFieldGUIWrapper modField = col.ExtendedProperties["ModField"] as ModFieldGUIWrapper;
            gridCol.SetValue(FrameworkElement.DataContextProperty, modField);
        }
        base.OnAutoGeneratedColumns(e);
    }

Any help is as always greatly appreciated. 一如既往,我们将不胜感激。

Cheers, Sean 肖恩干杯

Found the reason why: 找到原因:

I should have set the DataGridColumn.Header to the data object that contains the property specified in the DataTrigger in order to function properly. 我应该已将DataGridColumn.Header设置为包含DataTrigger中指定的属性的数据对象,以使其正常运行。 Then, I just overrided the ToString() method of my data class to display the pretty name that I wanted. 然后,我只是重写了数据类的ToString()方法以显示所需的漂亮名称。

protected override void OnAutoGeneratedColumns(EventArgs e)
    {
        var dataTable = pivotMod.DataTable;
        foreach (DataGridColumn gridCol in Columns)
        {
            var colName = gridCol.Header.ToString();
            DataColumn col = dataTable.Columns[colName];

            // set the datacontext of the gridcolumn to the modfield ...
            ModFieldGUIWrapper modField = col.ExtendedProperties["ModField"] as ModFieldGUIWrapper;
            gridCol.SetValue(FrameworkElement.DataContextProperty, modField);

            // set the header to the data object so that the datatrigger's binding works!!
            gridCol.Header = modField;

        }
        base.OnAutoGeneratedColumns(e);
    }

Thanks to Chris W. your comment set me on the right path! 感谢克里斯·W。您的评论使我走上了正确的道路! :) :)

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

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