문제

I'm trying to load the foillowing xaml:

<HierarchicalDataTemplate ItemsSource="{Binding Items}">
    <CheckBox Checked="CheckBox_Checked" Tag="{Binding Champ1}" Unchecked="CheckBox_Unchecked">
        <CheckBox.Template>
            <ControlTemplate>
                <TextBlock Background="{Binding Path=Background, RelativeSource={RelativeSource AncestorType=CheckBox}}" Text="{Binding Champ1}" />
            </ControlTemplate>
        </CheckBox.Template>
    </CheckBox>
</HierarchicalDataTemplate>

using this in the constructor of my treeview:

string template = "<HierarchicalDataTemplate ItemsSource=\"{Binding Items}\">" +
                    "<CheckBox Checked=\"CheckBox_Checked\"" +
                              " Tag=\"{Binding Champ1}\"" +
                              " Unchecked=\"CheckBox_Unchecked\">" +
                        "<CheckBox.Template>" +
                            "<ControlTemplate>" +
                                "<TextBlock Background=\"{Binding Path=Background," +
                                                                "RelativeSource={RelativeSource AncestorType=CheckBox}}\"" +
                                           "Text=\"{Binding Champ1}\" />" +
                            "</ControlTemplate>" +
                        "</CheckBox.Template>" +
                    "</CheckBox>" +
               " </HierarchicalDataTemplate>";

this.ItemTemplate = (HierarchicalDataTemplate)XamlReader.Parse(template);

I'm getting this error:

'Impossible de créer le type inconnu 'HierarchicalDataTemplate'.' numéro de ligne '1' et position de ligne '2'.

(My machine being configured in french, this message means : "Unable to create the unknown type HierarchicalDataTemplate").

Does someone have an idea why ? The xaml snippet works perfectly when written directly in the xaml file.

Thanks !


Edit

I had a test with a simple version:

string template = "<HierarchicalDataTemplate ItemsSource=\"{Binding Items}\">" +
                                "<TextBox Text=\"Test\"/>"+
                            " </HierarchicalDataTemplate>";

and the problem is the same.


Edit2

Ok the problem is wider and seems unrelated to the `HierarchicalDataTemplate'

string test = "<TextBox Text=\"Test\" />";
var ret = XamlReader.Parse(test);

Produces the same error...

도움이 되었습니까?

해결책

Ok I found the problem(s).

As John Bowen sait, the first problem are the event handlers wich can't be processed. The second problem is that the main namespace is missing in the xaml.

So this code works:

string template = "<HierarchicalDataTemplate  xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" ItemsSource=\"{Binding Items}\">" +
                    "<CheckBox Tag=\"{Binding Champ1}\" >" +
                        "<CheckBox.Template>" +
                            "<ControlTemplate>" +
                                "<TextBlock Background=\"{Binding Path=Background," +
                                                                "RelativeSource={RelativeSource AncestorType=CheckBox}}\"" +
                                           " Text=\"{Binding Champ1}\" />" +
                            "</ControlTemplate>" +
                        "</CheckBox.Template>" +
                    "</CheckBox>" +
               " </HierarchicalDataTemplate>";

this.ItemTemplate = (DataTemplate)XamlReader.Parse(template);

다른 팁

You can't use event handlers with XamlReader because they are required to be located in the XAML's code-behind, which obviously doesn't exist in parsed XAML.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top