本文介绍了如何在WPF ListView中的特定列居中文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想这也是的HorizontalAlignment,而不是TextAlignment但他们仍然显示对齐到左。
<窗口x:类=EditorWindow.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的DataContext ={绑定的RelativeSource = {的RelativeSource自}}
标题=主窗口高度=800宽度=600>
<电网>
< ListView控件的ItemsSource ={结合效果}>
< ListView.View>
< GridView控件>
< GridViewColumn WIDTH =100标题=名称DisplayMemberBinding ={结合名}/>
< GridViewColumn WIDTH =100标题=类型>
< GridViewColumn.CellTemplate>
<的DataTemplate>
< TextBlock的文本={绑定类型}TextAlignment =中心/>
< / DataTemplate中>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
< GridViewColumn WIDTH =100标题=透明度DisplayMemberBinding ={结合不透明}/>
< / GridView的>
< /ListView.View>
< / ListView控件>
< /网格>
< /窗>
解决方案
尝试设置 HorizontalContentAlignment
到拉伸
的该ItemContainerStyle。然后,它应该与任何 TextAlignment =中心
或的HorizontalAlignment =中心
为的TextBlock
< ListView控件的ItemsSource ={结合效果}>
< ListView.ItemContainerStyle>
<风格的TargetType =ListViewItem的>
< setter属性=HorizontalContentAlignmentVALUE =拉伸/>
< /样式和GT;
< /ListView.ItemContainerStyle>
<!--...-->
< / ListView控件>
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>
解决方案
Try to set HorizontalContentAlignment
to Stretch
for the ItemContainerStyle. Then it should work with either TextAlignment="Center"
or HorizontalAlignment="Center"
for the TextBlock
<ListView ItemsSource="{Binding Effects}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
<!--...-->
</ListView>
这篇关于如何在WPF ListView中的特定列居中文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!