I was able to recreate this XAML DataGridTextColumn:
<tk:DataGridTextColumn
Binding="{Binding FirstName}"
Header="First Name"/>
in code like this:
DataGridTextColumn dgtc = new DataGridTextColumn();
dgtc.Header = propertyLabel;
dgtc.Binding = new Binding(propertyName);
theDataGrid.Columns.Add(dgtc);
But how do I recreate the following DataGridTemplateColumn in code?
<tk:DataGridTemplateColumn Width="100">
<tk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"/>
<TextBlock Text=" "/>
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"/>
</StackPanel>
</DataTemplate>
</tk:DataGridTemplateColumn.CellTemplate>
</tk:DataGridTemplateColumn>
i.e. I'm getting stuck on defining the CellTemplate:
DataGridTemplateColumn dgTemplateColumn = new DataGridTemplateColumn();
dgTemplateColumn.CellTemplate = new CellTemplate ...???
Answer:
Thank you Aran, just referring to the template key in XAML works well for what I needed, here is how I changed the above to work for me:
XAML:
<Window.Resources>
<DataTemplate x:Key="manageAreaCellTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"/>
<TextBlock Text=" "/>
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
code-behind:
DataGridTemplateColumn dgTemplateColumn = new DataGridTemplateColumn();
dgTemplateColumn.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
dgTemplateColumn.Header = "Manage Options";
dgTemplateColumn.CellTemplate = this.FindResource("manageAreaCellTemplate") as DataTemplate;
theDataGrid.Columns.Add(dgTemplateColumn);