How to center and right align text inside a GridViewColumn in a WPF ListView Control
I had some issues today getting some values inside a ListView / GridView to center and right align. Regardless of what properties I set on the columns / data templates / etc it would always remain left aligned for text blocks.
To resolve you have to set the style at the list view level. Changing the horizontal content alignment to stretch allows the inner controls (text block) to align based on settings.
<ListView <ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> </Style> </ListView.ItemContainerStyle> <ListView.View> <GridView> <GridViewColumn> <TextBlock Text="MyText" TextAlignment="Center" /> </GridViewColumn> </GridView> <ListView.View> </ListView>
Thanks to Bill Menees for helping find this solution.