0

找不到类型“类型”。[行:7 位置:21]

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

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

        }
4

2 回答 2

1

导致该错误的原因是 XAML 分析器无法将 XAML 中的类型解析x:Type为有效的 CLR 类型,可能是因为 XAML 读取器在没有适当上下文的情况下无法正确处理 XAML 中的命名空间映射。

我有一个自定义版本,使用 aParserContext来定义 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);
于 2015-03-13T17:59:38.880 回答
1

你有一个Silverlight项目。Silverlight 不支持标记扩展x:Type。Silverlight 中的祖先绑定如下所示:

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

[编辑]顺便说一句,你不能绑定到ActualWidth. 你必须观察SizeChanged事件并有一些处理代码。你会在这里找到解决这个问题的非常优雅的方法: binding-to-actualwidth

于 2015-03-16T10:36:50.677 回答