DataGrid 右侧的 DataGrid 行 headers
DataGrid row headers on right side of DataGrid
是否可以让 DataGrid 的行 headers 出现在网格的右侧?
我试图为 DataGrid 找到一些控件模板,但其中 none 允许更改行的位置 headers。
谢谢
编辑:图片中,我要转这个
进入这个
参考:http://blog.magnusmontin.net/2014/08/18/right-aligned-row-numbers-datagridrowheader-wpf/
public class Country
{
public string Name { get; set; }
public string Continent { get; set; }
}
<DataGrid ItemsSource="{Binding Countries}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Country" Binding="{Binding Name}"/>
</DataGrid.Columns>
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding DataContext.Continent,
RelativeSource={RelativeSource AncestorType=DataGridRow}}"></TextBlock>
</DataTemplate>
</DataGrid.RowHeaderTemplate>
<DataGrid.RowHeaderStyle>
<Style TargetType="{x:Type DataGridRowHeader}">
<!-- Override ControlTemplate -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridRowHeader}">
<Grid xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2">
<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}"
HorizontalAlignment="Right">
<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>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowHeaderStyle>
这不是一个理想的解决方案,而是一种解决方法,我最终添加了一列,将其绑定到我想放入行 header 中的任何内容,并将其样式设置为看起来像行 headers列。
是否可以让 DataGrid 的行 headers 出现在网格的右侧? 我试图为 DataGrid 找到一些控件模板,但其中 none 允许更改行的位置 headers。 谢谢
编辑:图片中,我要转这个
进入这个
参考:http://blog.magnusmontin.net/2014/08/18/right-aligned-row-numbers-datagridrowheader-wpf/
public class Country
{
public string Name { get; set; }
public string Continent { get; set; }
}
<DataGrid ItemsSource="{Binding Countries}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Country" Binding="{Binding Name}"/>
</DataGrid.Columns>
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding DataContext.Continent,
RelativeSource={RelativeSource AncestorType=DataGridRow}}"></TextBlock>
</DataTemplate>
</DataGrid.RowHeaderTemplate>
<DataGrid.RowHeaderStyle>
<Style TargetType="{x:Type DataGridRowHeader}">
<!-- Override ControlTemplate -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridRowHeader}">
<Grid xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2">
<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}"
HorizontalAlignment="Right">
<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>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowHeaderStyle>
这不是一个理想的解决方案,而是一种解决方法,我最终添加了一列,将其绑定到我想放入行 header 中的任何内容,并将其样式设置为看起来像行 headers列。