简体   繁体   English

WPF DataGrid列标题使用自定义样式调整大小

[英]WPF DataGrid Column Header Resize with Custom Style

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 : 我有一个带有自定义模板列和标题样式的WPF DataGrid(.NET 4),并希望能够调整列的大小:

<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>

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. 我已经看到了这个答案并查看了Thumb控件,但是这似乎是重复已经提供的功能的过度杀伤。 The MSDN blog post references a StaticResource - RowHeaderGripperStyle which they don't provide! MSDN博客文章引用了一个他们没有提供的StaticResource - RowHeaderGripperStyle!

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>

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

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