This question is similar to others that have been posted, but none seem to offer the solution that works here.
Problem:
Trying to create a DataGridTemplateColumn
at runtime with a particular DataTemplate.
Just want a CheckBox in the DataTemplate.
For various reasons, I can't do it in XAML for this application, and I know that using FrameworkElementFactory
is deprecated.
Here's what I have:
string dt = "<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:WpfApplication40" >
<CheckBox x:Name="dtCheckBox" Checked="CheckBox_Checked" /></DataTemplate>
DataGridTemplateColumn tc1 = new DataGridTemplateColumn();
DataTemplate datatemplate = (DataTemplate)System.Windows.Markup.XamlReader.Parse(dt);
tc1.CellTemplate = datatemplate;
dataGrid1.Columns.Add(tc1);
In the containing class:
public void CheckBox_Checked(object sender, RoutedEventArgs e){}
At runtime, I keep getting a message box:
"Failed to create a 'Checked' from the text 'CheckBox_Checked'".
Obviously it can't figure out where the CheckBox_Checked
handler is in the class, but why?
If I take out the Checked="CheckBox_Checked"
it works fine.
Thanks for your insights.