XamlReader.Parse不';我似乎没有使用HierarchycalDataTemplate
我正在尝试加载以下xaml:XamlReader.Parse不';我似乎没有使用HierarchycalDataTemplate,xaml,hierarchicaldatatemplate,xamlreader,Xaml,Hierarchicaldatatemplate,Xamlreader,我正在尝试加载以下xaml: <HierarchicalDataTemplate ItemsSource="{Binding Items}"> <CheckBox Checked="CheckBox_Checked" Tag="{Binding Champ1}" Unchecked="CheckBox_Unchecked"> <CheckBox.Template> <ControlTemplate>
<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>
在我的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);
string template=“”+
"" +
"" +
"" +
"" +
"" +
"" +
"" +
" ";
this.ItemTemplate=(hierarchycaldatatemplate)XamlReader.Parse(template);
我得到了这个错误:
“HierarchycalDataTemplate”中的“不可能的数据类型”
编号“1”和位置“2”
(我的机器是用法语配置的,此消息表示:“无法创建未知类型HierarchycalDataTemplate”)
有人知道为什么吗?
当直接在xaml文件中编写时,xaml代码段可以完美地工作
谢谢
编辑 我用一个简单的版本进行了测试:
string template = "<HierarchicalDataTemplate ItemsSource=\"{Binding Items}\">" +
"<TextBox Text=\"Test\"/>"+
" </HierarchicalDataTemplate>";
string template=“”+
""+
" ";
问题是一样的
编辑2 好的,问题更广泛,似乎与“HierarchycalDataTemplate”无关
string test = "<TextBox Text=\"Test\" />";
var ret = XamlReader.Parse(test);
string test=”“;
var ret=XamlReader.Parse(测试);
产生相同的错误…您不能将事件处理程序与
XamlReader一起使用,因为它们必须位于XAML的代码后面,而在解析的XAML中显然不存在这些代码。好的,我发现了问题
正如sait所说,第一个问题是无法处理的事件处理程序。
第二个问题是xaml中缺少主命名空间
因此,该代码起作用:
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);
string template=“”+
"" +
"" +
"" +
"" +
"" +
"" +
"" +
" ";
this.ItemTemplate=(DataTemplate)XamlReader.Parse(template);
我刚刚测试了删除处理程序,错误消息仍然相同。事实上,我还使用了更简单的版本进行了测试:string template=“”+”代码>和它仍然失败,并显示相同的错误消息。