0

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.

Evan L
  • 3,805
  • 1
  • 22
  • 31
edtheprogrammerguy
  • 5,957
  • 6
  • 28
  • 47
  • Isn't `Checked` expecting a `bool` value? I was under the impression that `Checked` was denoting if the `CheckBox` defaults checked or unchecked. Try `true` or `false`. Of course I could be totally wrong. – Evan L May 23 '13 at 21:04
  • `Checked` is an event looking for a class method (RoutedEventHandler) – edtheprogrammerguy May 23 '13 at 21:31
  • Yep... just read the documentation. I was thinking of `isChecked`. My apologies. Looking for a viable answer now =) – Evan L May 23 '13 at 21:41

0 Answers0