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.

About these ads
  1. Tom
    February 12, 2012 at 3:18 am | #1

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

  2. December 18, 2012 at 12:56 pm | #2

    Very good, thank you for the tip. In my case I needed to add the setter at the top of my xaml view based upon another style that we are using for the container:

    Thank you,

  1. No trackbacks yet.
You must be logged in to post a comment.
Follow

Get every new post delivered to your Inbox.

Join 164 other followers

%d bloggers like this: