WPF 中的复杂 header 样式

Complex header styled in WPF

是否可以采取这样的方式:

<DataGridTextColumn.Header>
    <StackPanel Orientation="Horizontal">
        <Label Content="BlahBlahBlah"/>
        <Expander x:Name="Blah_Filter"/>
    </StackPanel>
</DataGridTextColumn.Header>

并使其成为一种样式,这样我就不必为每一列都打字了。如果可能的话,我也想单独命名这些列。如有任何帮助,我们将不胜感激。

您可以将 header ContentTemplate 更改为

<Window.Resources>
    <Style TargetType="{x:Type DataGridColumnHeader}"
           x:Key="ColumnHeaderTemplate">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="{Binding}" />
                        <Expander />
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<DataGrid>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Column1Content}"
                            HeaderTemplate="{StaticResource ColumnHeaderTemplate}"
                            Header="BlahBlahBlah1" />
        <DataGridTextColumn Binding="{Binding Column2Content}"
                            HeaderTemplate="{StaticResource ColumnHeaderTemplate}"
                            Header="BlahBlahBlah2" />
    </DataGrid.Columns>
</DataGrid>

其中 Column1ContentColumn2Content 是您的单元格数据的绑定,BlahBlahBlah1BlahBlahBlah1 是 header 标题。