如何在 WPF ListView 中的特定列中居中文本?
我尝试了这个,也尝试了 HorizontalAlignment,而不是 TextAlignment,但它们仍然显示为左对齐。
<Window x:Class="EditorWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="800" Width="600">
<Grid>
<ListView ItemsSource="{Binding Effects}">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="100" Header="Type" >
<GridViewColumn.CellTemplate >
<DataTemplate>
<TextBlock Text="{Binding Type}" TextAlignment="Center"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="100" Header="Opacity" DisplayMemberBinding="{Binding Opacity}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
I tried this and also HorizontalAlignment, instead of TextAlignment but they still show up aligned to left.
<Window x:Class="EditorWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="800" Width="600">
<Grid>
<ListView ItemsSource="{Binding Effects}">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="100" Header="Type" >
<GridViewColumn.CellTemplate >
<DataTemplate>
<TextBlock Text="{Binding Type}" TextAlignment="Center"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="100" Header="Opacity" DisplayMemberBinding="{Binding Opacity}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将 ItemContainerStyle 的
HorizontalContentAlignment
设置为Stretch
。然后它应该与TextAlignment="Center"
或HorizontalAlignment="Center"
一起用于TextBlock
Try to set
HorizontalContentAlignment
toStretch
for the ItemContainerStyle. Then it should work with eitherTextAlignment="Center"
orHorizontalAlignment="Center"
for theTextBlock