How to left align or right align the header in a ListView or GridView in WPF

So you have this great ListView or GridView in your fancy WPF application, but everything is centered. Maybe you like that, and all is good. Or, like most of us, you want the data aligned to the left or right. I like the left for most data, makes more sense. You looked for the property, and hmm… not to be found.

It turns out this seemingly simple task is, well, actually quite simple. It is really easy to spend a number of hours looking for the “magic” tag, property, etc. to do this. You will find out though, it’s all about having style.

You simply need to set the HorizontalContentAlignment on the style for the header to align everything however you want to.

Take a look at the following code:

<GridViewColumn  Width="Auto"  >
    <Style  TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="HorizontalContentAlignment" Value="Right" />

What we are doing is setting the HorizontalContentAlignment to right aligned. This will cause the contents of the header to all shift to the right. If you are using your own template, make sure to right align the textblocks and other controls as well. Otherwise, it probably won’t look right.

Nice and easy, right?

Happy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.