Home > .Net Framework, C#, WPF > How to center and right align text inside a GridViewColumn in a WPF ListView Control

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.

Advertisement
  1. Tom
    February 12, 2012 at 3:18 am | #1

    I’ve been looking everywhere for this!!! Many thanks for posting!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.