Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGrid Row Header WPF

I have a populated DataTable (xDataTable) with 7 defined columns - the first column I want to use as my RowHeader.

I also have a DataGrid:

 <DataGrid x:Name="DataGridX" ItemsSource="{Binding}" Grid.Row="0" 
           CanUserAddRows="False" SelectionUnit="Cell" />

I then set the DataContext of my DataGrid:

DataGridX.DataContext = xDataTable;

This all works - but how can I set the first column of my DataGrid as a RowHeader?

like image 251
JoelH Avatar asked Feb 09 '16 09:02

JoelH


1 Answers

Use the below style (usual case):

<DataGrid.RowHeaderStyle>
    <Style TargetType="{x:Type DataGridRowHeader}">
        <Setter Property="Content" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},Path=Columns[0].Header,Mode=OneTime}" />
    </Style>
</DataGrid.RowHeaderStyle>

If we want separate RowHeader for separate Row then use:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="IsSelected" Value="{Binding IsRowSelected}" />
        <Setter Property="Header" Value="{Binding Content}" />
    </Style>
</DataGrid.RowStyle>

Just change the above binding as required.

If the first column is:

<DataGridTextColumn Binding="{Binding Content}" Header="Content"/>

then remove this column and use this binding for the Header.

like image 95
Kylo Ren Avatar answered Oct 16 '22 08:10

Kylo Ren