I am building a project in WPF , in which the datagrid is getting filled on a combobox selection changed event , based on the selected item , it fetches the corresponding data from the TFS , and populates the datagrid.
Now there are about 4 columns , where I want the filtering functionality. So , I referred to a page from jarloo . com for excel like autofiltering, and used the same technique , to create a toggle button , and a popup , but I had to create a controltemplate for that , and based on some specific column headers , I applied the template in the respective columns , as shown below : (I have minimized the code for better readability)
<UserControl.Resources><ControlTemplate x:Key="CustomHeader" TargetType="DataGridColumnHeader">
<Grid>
<TextBlock Text="Binding"/>
<ToggleButton x:Name="btnFilter">
<Popup x:Name="popupFilter">
<Grid>
<CheckBox x:Name="SelectAll" Content="SelectAll"
IsChecked="True"></CheckBox>
<ListBox x:Name="lbFilteredDAta"></ListBox>
</Grid>
</Popup>
</ToggleButton>
</Grid>
</ControlTemplate>
</UserControl.Resources>
<Grid Margin="0,0,0,10">
<DataGrid x:Name="dgResult">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Style.Triggers>
<Trigger Property="Content" Value="FileName">
<Setter Property="Template" Value="{StaticResource CustomHeader}"/>
</Trigger>
<Trigger Property="Content" Value="FilePath">
<Setter Property="Template" Value="{StaticResource CustomHeader}"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
</Grid>
So now , what I want is , after the datagrid is bound to the itemsource , and the templates are created from the trigger events , the listbox present inside the popup for each togglebutton in the two columns , should populate with the corresponding distinct datagrid values for that particular column ,like Popup for the FileName column should populate values for that column.
But I am unable to retrieve the child element of the column header template from the C# code behind. SO I am unsure how to retrieve and manipulate the listbox element from the code behind. The example link given above , has hard coded everything , so it is very easy that way. Can you please let me know the simplest way around this?