How to add handler in dynamic datatemplate
- by Phillip Ngan
I am successfully declaring a data template in a code behind as follows:
private static DataTemplate CreateTemplate(string sortMemberPath, HorizontalAlignment horzAlignment)
{
const string xamlFormat
=
"<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" >"
+ "<StackPanel > "
+ " <TextBlock Margin=\"2,0\" VerticalAlignment=\"Center\" HorizontalAlignment=\"_HALIGNMENT_\" "
+
" Text=\"hello there\"> "
+ " </TextBlock> "
+ "</StackPanel>"
+ "</DataTemplate>";
return (DataTemplate) XamlReader.Load(xamlReturned);
}
But now I want to add a size changed handler by changing the line:
+ "<StackPanel > "
to
+ "<StackPanel SizeChanged="SizeChangedHandler" > "
I have the method "SizeChangedHandler" declared in the code behind. This results in a xaml parse error when the control attempts to load at runtime. I suspect that it can't find the handler "SizeChangedHandler". How can I specify this handler so that the xaml parser is happy.