I'm currently implementing filter capabilities on a DataGrid. I redefined the DataGridColumnHeader as follows (the ToggleButton gives access to a Popup which I don't detail and simplify the code for clarity).
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ContentControl Content="{Binding}" />
<ToggleButton Name="FilterToggleButton" Grid.Column="1" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
All columns are specified as follow :
<DataGridTemplateColumn Header="Example">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Example}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I would like to access the ToggleButton from code-behing corresponding to the column with a given Header value. I'm able to identify the column where dataGrid is the name of the DataGrid. I believe there is no direct link between column and it's header ContentTemplate as column.HeaderTemplate is null. How do I get the FilterToggleButton from the column?
foreach (var column in dataGrid.Columns)
{
var header = column.Header; // String with cast required, but I expected it to be the ContentTemplate.
if (header == lookupHeader)
{
// How do I get the FilterToggleButton from the column?
}
}
I also tried to first list all toggle buttons var tgls = UIHelpers.FindChildren<ToggleButton>(dataGrid); and then used the #well-know# UI FindParent(), FindChildren() to get the corresponding header without success.
Alright, I just came up with an idea which seems to work event though there are probably way cleaner codes! Just wondering why there is one toggle button more that the column headers (probably for the default row number column which I don't show?)