WPF DataGrid中行头宽度的调整手柄大小调整

4

我有一个WPF DataGrid(System.Windows.Controls)。

可以调整列标题和行高度的大小,但我似乎找不到让用户调整行标题宽度的方法。我希望DataGrid在打开时具有固定的行标题宽度大小,并允许用户根据需要调整行标题的宽度,但是没有调整手柄可以实现这一点。

有什么想法吗?

谢谢!

2个回答

2

经过快速在线搜索,我发现有两种方法可以实现您需求的第一部分。你可以像这样打开你的 DataGrid 并设置一个固定的 RowHeader Width:

<DataGrid ItemsSource="{Binding YourItems}" RowHeaderWidth="100">

或者像这样:
<DataGrid ItemsSource="{Binding YourItems}">
    <DataGrid.RowHeaderStyle>
        <Style TargetType="{x:Type DataGridRowHeader}">
            <Setter Property="Width" Value="100" />
        </Style>
    </DataGrid.RowHeaderStyle>
</DataGrid>

RowHeaderStyle 显然让我们在 DataGridRowHeader 上设置更多的属性,但不幸的是,我找不到任何让用户自己调整大小的方法。


我已经做到了这一步,但是对我来说很奇怪的是,在WPF中没有简单的方法来获取调整大小的行为,我认为在winforms中实现这一点相当容易。 - Nadav
是的,当我在搜索时,我发现了许多解决方案,但只适用于旧的“WinForms”控件。 - Sheridan

0
我曾经遇到同样的问题。下面是我的解决方案:
复制 RowHeaderStyle 并进行扩展:
<UserControl.Resources>
    <Style x:Key="RowHeaderStyle">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRowHeader}">
                    <Grid>
                        <Themes:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" IsSelected="{TemplateBinding IsRowSelected}" Orientation="Horizontal" Padding="{TemplateBinding Padding}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}">
                            <StackPanel Orientation="Horizontal">
                                <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
                                <Control SnapsToDevicePixels="False" Template="{Binding ValidationErrorTemplate, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGridRow}}}">
                                    <Control.Visibility>
                                        <Binding Path="(Validation.HasError)" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGridRow}}">
                                            <Binding.Converter>
                                                <BooleanToVisibilityConverter/>
                                            </Binding.Converter>
                                        </Binding>
                                    </Control.Visibility>
                                </Control>
                            </StackPanel>
                        </Themes:DataGridHeaderBorder>
                        <Thumb x:Name="PART_TopHeaderGripper" VerticalAlignment="Top">
                            <Thumb.Style>
                                <Style TargetType="{x:Type Thumb}">
                                    <Setter Property="Height" Value="8"/>
                                    <Setter Property="Background" Value="Transparent"/>
                                    <Setter Property="Cursor" Value="SizeNS"/>
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type Thumb}">
                                                <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"/>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </Thumb.Style>
                        </Thumb>
                        <Thumb x:Name="PART_BottomHeaderGripper" VerticalAlignment="Bottom">
                            <Thumb.Style>
                                <Style TargetType="{x:Type Thumb}">
                                    <Setter Property="Height" Value="8"/>
                                    <Setter Property="Background" Value="Transparent"/>
                                    <Setter Property="Cursor" Value="SizeNS"/>
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type Thumb}">
                                                <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"/>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </Thumb.Style>
                        </Thumb>
                        <!-- This is the part for changing the width -->
                        <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" DragDelta="PART_RightHeaderGripper_DragDelta" Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}">

                            <Thumb.Style>
                                <Style TargetType="{x:Type Thumb}">
                                    <Setter Property="Width" Value="8"/>
                                    <Setter Property="Background" Value="Transparent"/>
                                    <Setter Property="Cursor" Value="SizeWE"/>
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type Thumb}">
                                                <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"/>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </Thumb.Style>
                        </Thumb>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <!-- If you want to wrap the RowHeaderText, use this block -->
        <Setter Property="DataGridRowHeader.ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock TextWrapping="Wrap" Text="{Binding}"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

在DataGrid中使用新的样式:

<Grid>
    <DataGrid x:Name="dgTest">
        <DataGrid.RowHeaderStyle>
            <Style BasedOn="{StaticResource RowHeaderStyle}"/>
        </DataGrid.RowHeaderStyle>
    </DataGrid>
</Grid>

代码后台:

private void PART_RightHeaderGripper_DragDelta(object sender, Primitives.DragDeltaEventArgs e)
{
    DataGrid dg = (sender as Thumb).Tag as DataGrid;
    dg.RowHeaderWidth = dg.RowHeaderActualWidth + e.HorizontalChange;
}

希望我能帮到你。


1
Themes 是从哪里来的? - Felix D.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接