WPF: Align text to the right

Aligning text to the right... sounds like a simple thing to do, apparently not that simple.

First, you have to define a CellTemplate and a DataTemplate for the GridViewColumn with an internal TextBlock that has it's HorizontalAlignment property set to "Right":

<GridViewColumn Header="Size" Width="80">
<TextBlock HorizontalAlignment="Right"
Text="{Binding Size}" />

Then you have to add a style resource to set the HorizontalContentAlignment property of ListViewItem elements to "Stretch":

<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment"

I can understand why it's like this though, basically you can put anything in those cells, and in some cases the alignment flag might not have any meaning... although it would have been very nice for the more simple apps...


brenda said…
you've tested this and it works? i can't seem to get it to work :(
David Elentok said…
Yeah, I tested before I wrote the post.
ghedgehog said…
Have viewed many other posts relating to this same problem, but this is the first one that has worked. The ListViewItem Style ....Stretch was missing in most other cases I think. Thanks a lot!
Anonymous said…
Thanks for the tip on the ListViewItem Style! I had been missing that as well, and couldn't get the alignments to work properly.

Popular posts from this blog

Restart the Windows File Sharing Service to fix weird problems

WPF, ImageSource and Embedded Resources

SharpDevelop dark color scheme