DataGrid HeaderTemplate binding problems

Sep 21, 2008 at 4:46 AM

I'm trying to figure out what I'm doing wrong...

I have a datatable that has two columns "Index" and "Value".
I'm creating a single column in datagrid  that I bind to "Value" column of a datatable.
I want to populate the rowheaders with "Index" column values.
I'm performing binding as follows

<Style TargetType="{x:Type dg:DataGridRow}">
        <Setter Property="Header" Value="{Binding Index

All of this works great. However I would like to create a more complex data template where I could put
a border, textblock, etc.
I tried to use the following approach:



Style TargetType="{x:Type dg:DataGridRow}">
    <Setter Property="HeaderTemplate">
                        <Border Margin="0,-1,0,0" Background="Blue"  HorizontalAlignment=" Stretch" BorderBrush="LightGray" BorderThickness="1,1,1,0">
<TextBlock Padding="3" Text="{Binding Index}" />


The binding does not work. The blue background border is created in every row header
however there is no text inside
 The error I get is:

System.Windows.Data Error: 39 : BindingExpression path error: 'Index' property not found on 'object' ''String' (HashCode=-1220323543)'. BindingExpression:Path=Index; DataItem='String' (HashCode=-1220323543); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')


Any suggestion on what I'm doing wrong here?
I would appreciate your help.



Sep 22, 2008 at 6:31 PM
Try putting back the Setter for Header you had originally (in addition to HeaderTemplate) and change the TextBlock's Text to {Binding}.

Sep 23, 2008 at 3:20 AM
Thanks Ben
your tip worked great!
Oct 6, 2008 at 1:31 AM

I have a followup question.

 I have a datagrid which has the row headers bound to "Index" column of a datable using the datatemplate described above.
This "Index" column has increasing numbers from 0 to 10000.
As I scroll down, the width of the rowheader increases when the number in the row header gets larger.
This is how it should be since, for example, "100" takes more space than "1", thus the rowheader becomes wider to accomodate it.
However if I scroll back up to the top of grid, the width of the rowheaders is not  reduced back to its orginal size
even though small numbers do not require so much space.

Is there a way to force the row headers to automatically resize back to their original size?
This is what the Excel grid does.

Thanks for your support




Oct 13, 2008 at 10:44 PM
The row headers, as currently designed, do not collapse back to smaller sizes (it's a general behavior of columns, not specific to row headers). This was to reduce the amount of fluctuation encountered with column widths while scrolling.

You can try to get the row headers to resize by setting RowHeaderWidth and then clearing the value, but I'm not sure if that will work. You'd also have to do this either as scrolling occurs or when scrolling completes.

Oct 14, 2008 at 6:33 PM
Thanks Ben, I will give it a try in scroll events