我正在尝试将ListView中的文本对齐到单元格的顶部。
这是我到目前为止所做的:
数据绑定如下:
<ListView.View>
<GridView>
<GridViewColumn Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListViewItem}},
Converter={StaticResource lviIndexToString}}"
Foreground="White"
VerticalAlignment="Top"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="80">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding LogTime}"
Foreground="{StaticResource ConsoleOrange}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="80">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding MessageType}"
Foreground="{Binding MessageType,
Converter={StaticResource logMsgToColor}}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Path=Message}"/>
</GridView>
</ListView.View>
ItemContainer
的样式如下:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Grid>
<GridViewRowPresenter VerticalAlignment="Stretch">
<GridViewRowPresenter.Resources>
<Style TargetType="ContentPresenter">
<Setter Property="Margin" Value="1"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
</Style>
</GridViewRowPresenter.Resources>
</GridViewRowPresenter>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
如果ContentPresenters VerticalAlignment设置为&#39; Stretch&#39;它可以正常工作。然而,视觉树显示ContentPresenter风格被某些东西所覆盖,但我无法通过什么来判断我的生活。
如果有人知道如何设置嵌套的ContentPresenters属性,或者教我如何设置自定义GridViewRowPresenter以更好地控制如何显示数据,那么我会非常感激。
如果在其他地方已经回答道歉,我很抱歉,但我的google-fu搜索(以及此处的搜索)没有透露任何有用的解决方案。
答案 0 :(得分:4)
要覆盖VerticalAlignment
属性ContentPresenter
的值,我们需要设置一些更高优先级的步骤。动画在precedence order中占据第2位。
<ListView.Resources>
<Style TargetType="ContentPresenter">
<Style.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard >
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="VerticalAlignment">
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<VerticalAlignment>Top</VerticalAlignment>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</ListView.Resources>
另一种方法是处理ContentPresenter.Loaded
事件并在那里设置属性值。
<Style TargetType="ContentPresenter">
<EventSetter Event="Loaded" Handler="ContentPresenter_Loaded"/>
</Style>
处理程序:
void ContentPresenter_Loaded(object sender, RoutedEventArgs e)
{
(sender as ContentPresenter).VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
}
使用Loaded
事件处理程序更改属性也可以使用Blend Behaviors / Attached Property
完成。