默认情况下,文字位于ListView
(内容单元格中不)标题的中心位置,我一直在努力让它保持对齐,这就是我的意思我想出了:
<ListView.Resources>
<DataTemplate x:Key="Templ">
<TextBlock HorizontalAlignment="Left" Text="{Binding}"/>
</DataTemplate>
</ListView.Resources>
...
<GridViewColumn HeaderTemplate="{StaticResource Templ}">File</GridViewColumn>
这似乎是改变标题外观的正确位置,因为我可以更改Margin
等其他属性,但它不会响应HorizontalAlignment
属性!我猜测文本框的大小适合内容并且本身居中,从而使对齐属性变得多余。
如何使文字左对齐?
答案 0 :(得分:21)
将HeaderContainerStyle设置为将HorizontalContentAlignment设置为左侧的样式:
<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>
答案 1 :(得分:4)
这是用于设置标题内容对齐的附加属性:
#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
答案 2 :(得分:0)
这可能会对其他人有所帮助,因为我也在努力挣扎,最好的方法是为GridViewColumn定义样式。
您可以设置一种样式以应用于所有列标题,如下所示
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Left" />
</Style>
</GridView.ColumnHeaderContainerStyle>
</GridView>
并且您可以覆盖特定列的标题样式,如下所示:
<GridViewColumn Header="Something">
<GridViewColumn.HeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Left" />
</Style>
</GridViewColumn.HeaderContainerStyle>
</GridViewColumn>