2015-03-13 71 views
0

未找到类型“类型”。 [行:7位置:21]Silverlight - XamlParseException未找到类型“类型”

我正试图动态生成一个数据模板。它工作正常,但如果我包含此属性,我会得到上述异常。

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); 

     } 
+0

您确定Visual Tree在发生错误时包含“telerik:GridViewCell”吗? – 2015-03-13 17:52:34

+0

@JamesHarcourt问题与此无关。错误消息清楚地表明XAML解析器找不到'Type'类型(实际上是'x:Type'或'System.Windows.Markup.TypeExtension')。 – 2015-03-13 17:55:17

+0

就我所知,基本上在解析这个XAML的时候没有可视化树,只要读者知道。 – 2015-03-13 17:57:34

回答

1

你有Silverlight项目。 Silverlight不支持标记扩展x:Type。在Silverlight祖先绑定是这样的:

{Binding Path=Foo, RelativeSource={RelativeSource AncestorType=UserControl}} 

[编辑] 而且顺便说一句,你不能绑定到ActualWidth。你必须观察SizeChanged事件并且有一些处理代码。你会发现这个问题here: binding-to-actualwidth相当优雅的解决方案。

1

因为XAML解析器不能在XAML解析类型x:Type为有效的CLR类型的错误造成的,可能是因为在XAML命名空间映射不能没有适当的上下文,由XAML阅读器正确处理。

我有this定制版本,它采用了ParserContext定义XML命名空间映射XAML:

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); 
+0

感谢您的回答。我刚刚意识到我指定了WPF,实际上它是一个silverlight项目。我会尝试查看您的链接是否也适用于我的场景。谢谢。目前我给你+1 – 2015-03-13 18:05:58

+0

我刚刚检查过,看起来像这个ParserContext使用XamlReader.Parse(显然)而不是负载。这是Silverlight中唯一可用的东西。无论如何,至少你已经解释了问题发生的原因。 – 2015-03-13 18:15:18