我有一个WPF DataGrid(System.Windows.Controls)。
可以调整列标题和行高度,但我似乎找不到让用户调整行标题宽度的方法。 我希望DataGrid以固定的行标题宽度大小打开,并让用户根据需要调整行标题的宽度,但是没有调整大小的抓取器。
有什么想法吗?
谢谢!
答案 0 :(得分:1)
在网上快速搜索后,我找到了两种方法来实现您的第一部分需求。您可以使用以下固定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
上设置了更多属性,但不幸的是,我找不到让用户自己调整大小的方法。
答案 1 :(得分: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;
}
希望,我可以帮忙。