Adding right-aligned row numbers to a DataGridRowHeader in WPF

Magnus Montin

This post provides an example of how you can right-align or centre the text in a DataGridRowHeader in a DataGrid in WPF using Visual Studio 2012 or later. It also explains how you can display the correct row numbers in the DataGridRowHeader and automatically update these as you are adding or removing items to and from the DataGrid’s ItemsSource collection dynamically.


The DataGrid control has a RowHeaderTemplate property that defines the DataTemplate for the DataGridRowHeader. A DataGridRowHeader is by default displayed to the left of the left-most data column on each row in the DataGrid. It doesn’t scroll horizontally with the rest of the columns and is usually used to display some additional information that associated with a specific row, for example a row number.

The RowHeaderTemplate DataTemplate doesn’t inherit the data context from the DataGridRow but since it gets added to the visual tree, you can use a…

View original post 777 more words


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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