DataGrid列标题的大小

DataGrid列标题的大小

本文介绍了使用自定义样式调整WPF DataGrid列标题的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有自定义模板列和标题样式的WPF DataGrid(.NET 4),并且希望能够调整列的大小:

I have a WPF DataGrid (.NET 4) with custom template columns and header styles and would like to be able to adjust the size of the columns :

<DataGridTemplateColumn.HeaderStyle>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="DataGridColumnHeader">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="Images\monitor.png" Width="16" Height="16"/>
                        <TextBlock Text="Hostname" TextWrapping="Wrap" Padding="3"/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DataGridTemplateColumn.HeaderStyle>

仍然可以对列进行排序和重新排列,但不能重新设置大小-抓手不会显示。我已经看到了答案,并查看了Thumb控件,但是,这似乎是重现已经提供的功能的过大杀伤力。 MSDN博客文章引用了他们不提供的StaticResource-RowHeaderGripperStyle!

Columns can still be sorted and re-arranged but not resized - the gripper does not show. I have seen this answer and looked at the Thumb control, however this seems like massive overkill to reproduce functionality already provided. The MSDN blog post references a StaticResource - RowHeaderGripperStyle which they don't provide!

推荐答案

我总是这样做,并且有效很好:

I always do it this way and it works pretty fine:

<Style TargetType="DataGridColumnHeader">
    <!-- here goes some setters -->

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="DataGridColumnHeader">
                <Grid Margin="{TemplateBinding Padding}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <!-- some stuff, like border etc. -->

                    <ContentPresenter />

                    <Thumb x:Name="PART_RightHeaderGripper" Grid.Column="1"
                        HorizontalAlignment="Right"
                        Width="2" BorderThickness="1"
                        BorderBrush="{Binding VerticalGridLinesBrush, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
                        Cursor="SizeWE"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这篇关于使用自定义样式调整WPF DataGrid列标题的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 08:54