【问题标题】:Resize gripper for row header width in WPF DataGrid调整 WPF DataGrid 中行标题宽度的抓手大小
【发布时间】:2013-12-09 22:23:38
【问题描述】:

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

可以调整列标题和行高的大小,但我似乎找不到让用户调整行标题宽度的方法。 我希望 DataGrid 以固定的行标题宽度大小打开,并让用户根据需要调整行标题的宽度,但没有调整大小的抓手。

有什么想法吗?

谢谢!

【问题讨论】:

  • +1 回答棘手的问题。

标签: wpf datagrid resize


【解决方案1】:

在网上快速搜索后,我找到了两种方法来实现您的第一部分要求。您可以使用固定的RowHeader Width 打开您的DataGrid,如下所示:

<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 中很容易实现。 .
  • 是的,在搜索时我遇到了许多解决方案,但仅适用于旧的 WinForms 控件。
【解决方案2】:

我遇到了同样的问题。这是我的解决方案:

复制 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;
}

希望,我能帮上忙。

【讨论】:

  • Themes 来自哪里?
猜你喜欢
  • 1970-01-01
  • 2012-12-25
  • 1970-01-01
  • 2011-11-20
  • 2011-08-20
  • 1970-01-01
  • 2011-03-05
  • 1970-01-01
  • 2011-09-25
相关资源
最近更新 更多