Skip to main content

Microsoft Silverlight

Answered Question Datatemplate event via XamlReaderRSS Feed

(0)

jkattestaart
jkattest...

Member

Member

24 points

38 Posts

Datatemplate event via XamlReader

Hello,

 

I'm trying to create a datatemplate column with an event, but it doesn't fire.

checkTemplate.Append("<DataTemplate ");

checkTemplate.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");

checkTemplate.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>");

checkTemplate.Append("<CheckBox IsChecked='{Binding &1}'/>");

checkTemplate.Append("</DataTemplate>");

checkEditTemplate.Append("<DataTemplate ");

checkEditTemplate.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");

checkEditTemplate.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>");

checkEditTemplate.Append("<CheckBox IsChecked='{Binding &1}' Click='CheckBox_Click'/>");

checkEditTemplate.Append("</DataTemplate>");

 ((DataGridTemplateColumn)column).CellTemplate = (DataTemplate)XamlReader.Load(checkTemplate.ToString().Replace("&1", field.Name));

((DataGridTemplateColumn)column).CellEditingTemplate = (DataTemplate)XamlReader.Load(checkEditTemplate.ToString().Replace("&1", field.Name));

private void CheckBox_Click(object sender, RoutedEventArgs e)

{

}

If i define the template through a resource the event does fire.

bryant
bryant

Star

Star

9937 points

1,629 Posts

Silverlight MVP
Answered Question

Re: Datatemplate event via XamlReader

The XamlReader doesn't support adding events. Instead, give your checkbox an x:Name and then once you add your Xaml to the tree you can find it and attach the event handler.

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

jkattestaart
jkattest...

Member

Member

24 points

38 Posts

Re: Datatemplate event via XamlReader

Bryant,

 

Thanks for your response. This seems to work but ...

checkTemplate.Append("<DataTemplate ");

checkTemplate.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");

checkTemplate.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>");

checkTemplate.Append("<CheckBox x:Name ='Check_&1' IsChecked='{Binding &1, Mode=TwoWay}'/>");

checkTemplate.Append("</DataTemplate>");

 

checkEditTemplate.Append(
"<DataTemplate ");

checkEditTemplate.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");

checkEditTemplate.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>");

checkEditTemplate.Append("<CheckBox x:Name ='EditCheck_&1' IsChecked='{Binding &1, Mode=TwoWay}'/>");

 

case "Boolean":

column = new DataGridTemplateColumn();

((DataGridTemplateColumn)column).CellTemplate = (DataTemplate)XamlReader.Load(checkTemplate.ToString().Replace("&1", field.Name));

((DataGridTemplateColumn)column).CellEditingTemplate = (DataTemplate)XamlReader.Load(checkEditTemplate.ToString().Replace("&1", field.Name));

 

:

:

column.Header = field.ShortLabel;

if (field.ColumnWidth != 0)column.Width = new DataGridLength(field.ColumnWidth);

dataGrid.Columns.Add(column);

if (field.DataType == "Boolean")

{

CheckBox cb = dataGrid.FindName("EditCheck_" + field.Name) as CheckBox;

cb.Click += new RoutedEventHandler(cb_Click);

}

 

The findname returns null, how do i find my control?

 

 

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24939 points

2,425 Posts

Microsoft
Answered Question

Re: Datatemplate event via XamlReader

Hi Jkat,

Unfortunately we can't. Controls in a DataTemplate reside in a separate name scope. The only way to modify the control is to create an event handler for the control, and use the sender parameter to get a reference of the control.

Best regards,

Jonathan 


Normal 0 7.8 pt 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;}

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities