调整样式化 WPF DataGrid 列的大小

Resize Columns of Styled WPF DataGrid

我有自定义样式的 DataGrid,我在调整列的大小时遇到​​了问题,因为它只有在光标恰好位于两列之间时才有效

这是设置 DataGridColumnHeader 的样式部分

    <Style TargetType="{x:Type DataGridColumnHeader}">
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="FontFamily" Value="Segoue UI"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="Height" Value="40"/>

    <Setter Property="Template">

        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                <Grid>
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="#6b6b6b">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>

                    <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" Style="{StaticResource ColumnHeaderGripperStyle}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

像这样增加 Thumb 的宽度:

 <Thumb x:Name="PART_RightHeaderGripper" Width="100" HorizontalAlignment="Right" Style="{StaticResource ColumnHeaderGripperStyle}"/>

您也可以在 运行 时间内更改它。这是例子

grd.Columns[1].Width =new DataGridLength(60); 

提及指数