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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With