I'm dynamically creating DataGrid columns (based on an event from my ViewModel) and programmatically adding them to an existing DataGrid. Each column uses a generic HeaderTemplate by setting it to a DataTemplate that has been identified in the xaml. The DataTemplate contains two labels in which needs their content needs to be changed upon creation of the DataGrid column. How would this be done? I understand that the DataTemplate uses the ContentPresenter but I am having trouble accessing it within a dynamically created DataGrid column. Code is as follows:
xaml: (template used to format the DataGrid column header):
<DataTemplate x:Key="columnTemplate">
<StackPanel>
<Label Padding="0" Name="labelA"/>
<Separator HorizontalAlignment="Stretch"/>
<Label Padding="0" Name="labelB"/>
</StackPanel>
</DataTemplate>
c#: (used to dynamically create a DataGrid column and add it to an existing DataGrid)
var dataTemplate = FindResource("columnTemplate") as DataTemplate;
var column = new DataGridTextColumn();
column.HeaderTemplate = dataTemplate;
DataGrid1.Columns.Add(column);
I would like to then access both labelA and labelB and change the content.