WPF: Setting the text alignment of a GridViewColumn
By default, text is centered in the headers of a ListView
(not in the content cells!), I've been struggling to make it left aligned, here's what I've come up with:
<ListView.Resources>
<DataTemplate x:Key="Templ">
<TextBlock HorizontalAlignment="Left" Text="{Binding}"/>
</DataTemplate>
</ListView.Resources>
...
<GridViewColumn HeaderTemplate="{StaticResource Templ}">File</GridViewColumn>
This does seem to be the right place to alter the appearance of the header, since I can change other properties like Margin
, etc., yet it does not respond to the HorizontalAlignment
property! I'm guessing the textbox is sized to content and itself开发者_开发百科 centered, thus making the alignment property redundant.
How can I make the text left aligned ?
Set the HeaderContainerStyle to a style that sets HorizontalContentAlignment to left:
<ListView.Resources>
<DataTemplate x:Key="Templ">
<TextBlock HorizontalAlignment="Left" Text="{Binding}"/>
</DataTemplate>
<Style x:Key="HeaderStyle" TargetType="GridViewColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn HeaderTemplate="{StaticResource Templ}" HeaderContainerStyle="{StaticResource HeaderStyle}">File</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
Here's an attached property for setting the header's content alignment:
#region HeaderContentAlignment
/// <summary>Gets a <see cref="GridViewColumn"/>'s header content alignment.</summary>
[Category("Common")]
[AttachedPropertyBrowsableForType(typeof(GridViewColumn))]
public static HorizontalAlignment GetHeaderContentAlignment(GridViewColumn gridViewColumn)
{
return (HorizontalAlignment)gridViewColumn.GetValue(HeaderContentAlignmentProperty);
}
/// <summary>Sets a <see cref="GridViewColumn"/>'s header content alignment.</summary>
public static void SetHeaderContentAlignment(GridViewColumn gridViewColumn, HorizontalAlignment value)
{
gridViewColumn.SetValue(HeaderContentAlignmentProperty, value);
}
/// <summary>Indicates a <see cref="GridViewColumn"/>'s <see cref="GridViewColumn.HeaderContainerStyle"/>.</summary>
public static readonly DependencyProperty HeaderContentAlignmentProperty =
DependencyProperty.RegisterAttached(
"HeaderContentAlignment",
typeof(HorizontalAlignment), // type
typeof(GridViewExtensions), // containing static class
new PropertyMetadata(HorizontalAlignment.Center, OnHeaderContentAlignmentChanged)
);
private static void OnHeaderContentAlignmentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
OnHeaderContentAlignmentChanged((GridViewColumn)d, (HorizontalAlignment)e.NewValue);
}
private static void OnHeaderContentAlignmentChanged(GridViewColumn gridViewColumn, HorizontalAlignment alignment)
{
Style style;
if (ColumnHeaderAlignments.TryGetValue(alignment, out style) == false)
{// style NOT created -> create, cache
style = new Style(typeof(GridViewColumnHeader));
style.Setters.Add(new Setter(Control.HorizontalContentAlignmentProperty, alignment));
ColumnHeaderAlignments.Add(alignment, style);
}
gridViewColumn.HeaderContainerStyle = style;
}
/// <summary>cache [HorizontalAlignment]: [Style]</summary>
static readonly Dictionary<HorizontalAlignment, Style> ColumnHeaderAlignments = new Dictionary<HorizontalAlignment, Style>(4);
#endregion HeaderContentAlignment
This may help someone else, as i was struggling too, the best way is to define style for GridViewColumn.
You can set a style to apply to all column headers like below
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Left" />
</Style>
</GridView.ColumnHeaderContainerStyle>
</GridView>
and You can override the header style for a specific column like below:
<GridViewColumn Header="Something">
<GridViewColumn.HeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Left" />
</Style>
</GridViewColumn.HeaderContainerStyle>
</GridViewColumn>
精彩评论