Page view counter
Datatemplate event via XamlReader Subscribe to this thread
Last post 11-28-2008 12:31 AM by Jonathan Shen – MSFT. 3 replies.
Sort Posts:
11-23-2008 10:35 AM
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.

jkattestaart

Loading...
Joined on 07-29-2007
Posts 25
11-23-2008 5:43 PM
Marked as Answer
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.

bryant

Loading...
Joined on 10-23-2002
Simi Valley, CA
Posts 1,153
11-24-2008 3:40 PM
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?

 

 

jkattestaart

Loading...
Joined on 07-29-2007
Posts 25
11-28-2008 12:31 AM
Marked as Answer
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.

Jonathan Shen – MSFT

Loading...
Joined on 06-18-2007
Posts 1,522
Microsoft Communities