I would like to set the header of a column with a converter that use a parameter. The value of the converter has to be the ItemsSource of the datagrid.
I know that to modify the text of the header I need to set the Text property of the textblock.
With this code, I can use a property in the view model of my view to set the header:
<DataGridTextColumn Binding="{Binding MyProperty}" Width="2.8cm">
<DataGridTextColumn.Header>
<TextBlock Text="{Binding DataContext.MyPropertyInViewModel,
RelativeSource={RelativeSource AncestorType={x:Type local:MyView}}}"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
However I prefer to use a converter for that. But I don't know how to use a converter inside the textblock of the column header. This converter need to get as value the object that is source of the datagrid.
Thanks.
You can get the DataGrid
's ItemsSource
by specifying it as RelativeSource
.
Try this
<DataGridTextColumn>
<DataGridTextColumn.Header>
<TextBlock Text="{Binding Path=ItemsSource,
RelativeSource={RelativeSource AncestorType=DataGrid,
Mode=FindAncestor},
Converter={StaticResource ValueConverter}}"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>