How to add handler in dynamic datatemplate

Posted by Phillip Ngan on Stack Overflow See other posts from Stack Overflow or by Phillip Ngan
Published on 2010-04-09T19:51:20Z Indexed on 2010/04/09 19:53 UTC
Read the original article Hit count: 442

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.

© Stack Overflow or respective owner

Related posts about silverlight-2.0

Related posts about datatemplate