I want to bind my datagrid header to a property on the DataContext of the grid. Now, I got it to work, but I consider this a temporarily solution:
<DataGrid x:Name="grid" ItemsSource="{Binding Path=Items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Description}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding ElementName=grid, Path=DataContext.ItemsUnit}"></TextBlock>
</DataTemplate>
The biggest issue with this solution is that it makes the binding more fragile (context sensitive). If the DataContext of the grid is used in a master/detail scenario (which makes the DataContext a BindingList instead of a single item) I would have to replace update the DataGrid DataContext with DataContext={Binding /}
.
Is there a more robust way to bind from the DataGrid.HeaderTemplate than using ElementName and refering to the DataContext?