Silverlight-XamlParseException类型';类型';找不到
找不到类型“type”。[第7行位置:21] 我正在尝试动态生成一个数据模板。它工作得很好,但是如果我包含这个属性,我会得到上面的异常Silverlight-XamlParseException类型';类型';找不到,xaml,silverlight,xamlreader,Xaml,Silverlight,Xamlreader,找不到类型“type”。[第7行位置:21] 我正在尝试动态生成一个数据模板。它工作得很好,但是如果我包含这个属性,我会得到上面的异常 Width="{Binding Path=ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:GridViewCell}}}" 完整的方法是: public DataTemplate GetTextColumnTemplate(
Width="{Binding Path=ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:GridViewCell}}}"
完整的方法是:
public DataTemplate GetTextColumnTemplate(int index)
{
string templateValue = @"
<DataTemplate
xmlns:sys=""clr-namespace:System;assembly=mscorlib""
xmlns:telerik=""http://schemas.telerik.com/2008/xaml/presentation""
xmlns=""http://schemas.microsoft.com/client/2007""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<StackPanel>
<TextBox Width=""{Binding Path=ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:GridViewCell}}}"" Text=""{Binding Path=V" + (index + 1).ToString() + @",Mode=TwoWay}"" AcceptsTab=""True"" AcceptsReturn=""True""/>
</StackPanel>
</DataTemplate>";
return (DataTemplate)XamlReader.Load(templateValue);
}
公共数据模板GetTextColumnTemplate(int索引)
{
字符串templateValue=@”
";
return(DataTemplate)XamlReader.Load(templateValue);
}
导致此错误的原因是XAML解析器无法将XAML中的类型
x:type
解析为有效的CLR类型,可能是因为XAML读取器无法在没有正确上下文的情况下正确处理XAML中的命名空间映射
我有一个自定义版本,它使用定义XAML的XML命名空间映射:
var context = new ParserContext {XamlTypeMapper = new XamlTypeMapper(new string[0])};
context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
//... And so on add other xmlns mappings here.
var template = (DataTemplate) XamlReader.Parse(yourXAMLstring, context);
您有一个
Silverlight
项目。Silverlight不支持标记扩展名x:Type
。Silverlight中的祖先绑定如下所示:
{Binding Path=Foo, RelativeSource={RelativeSource AncestorType=UserControl}}
[编辑]
顺便说一句,您不能绑定到
实际宽度
。您必须观察SizeChanged
事件,并编写一些处理代码。您将找到解决此问题的非常优雅的方法。您确定在发生错误时,可视化树中包含“telerik:GridViewCell”吗?@JamesHarcourt问题与此无关。错误消息明确指出XAML解析器找不到类型type
(实际上是x:type
,或者System.Windows.Markup.TypeExtension
)。据我所知,就读者所知,在解析这个XAML时基本上没有可视化树。我不太确定@HighCore——OP明确指出,只有当他尝试将文本框的width属性绑定到找到可视化树的第一个telerik:GridViewCell的实际width属性时,才会发生错误树。@JamesHarcourt看到我的答案了。谢谢你的回答。我刚刚意识到我指定了WPF,事实上,它是一个silverlight项目。我将尝试看看你的链接是否也适用于我的场景。谢谢现在我给你+1,我刚刚检查过,这个ParserContext似乎使用了XamlReader.Parse(显然)而不是加载。这是我在silverlight唯一能找到的东西。不管怎样,至少你已经解释了问题发生的原因。