0

The type 'Type' was not found. [Line: 7 Position: 21]

I'm trying to dynamically generate a datatemplate. it works fine, but if I include this attribute, I get the above exception.

Width="{Binding Path=ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:GridViewCell}}}"

And the complete method:

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

        }
Slime recipe
  • 2,223
  • 3
  • 32
  • 49
  • Are you sure the Visual Tree contains a "telerik:GridViewCell" in the case when the error occurs? – James Harcourt Mar 13 '15 at 17:52
  • @JamesHarcourt the problem doesn't have anything to do with that. The error message clearly states that the XAML parser can't find the type `Type` (which is `x:Type` actually, or `System.Windows.Markup.TypeExtension`). – Federico Berasategui Mar 13 '15 at 17:55
  • As far as I understand, basically there is no visual tree at the moment of parsing this XAML as far as the reader aware. – Slime recipe Mar 13 '15 at 17:57
  • I wouldn't be so sure @HighCore - the OP clearly states that the error only occurs when he includes an attempt to bind the width property of his TextBox to the ActualWidth property of the first telerik:GridViewCell found up the Visual Tree. – James Harcourt Mar 13 '15 at 17:58

2 Answers2

1

The error is caused because the XAML parser can't resolve the type x:Type in XAML to a valid CLR type, probably because namespace mappings in XAML cannot be properly processed by the XAML reader without proper context.

I have a customized version of this which uses a ParserContext to define the XML namespace mappings for 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);
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • thanks for the answer. I just realized that I specified WPF and in fact, it is a silverlight project. I will try to see if your link will apply to my scenario as well. thanks. for the moment I'll give you +1 – Slime recipe Mar 13 '15 at 18:05
  • I just checked, it seems like this ParserContext uses the XamlReader.Parse (obviously) and not the Load. which is the only thing available to me in silverlight. anyway, at least you've explained why the problem happens. – Slime recipe Mar 13 '15 at 18:15
1

You have a Silverlight project. Silverlight does not support the markup extension x:Type. Ancestor bindings in Silverlight look like this:

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

[Edit] And btw you can't bind to ActualWidth. You have to observe the SizeChanged event and have some handling code. You will find quite elegant solutions to this problem here: binding-to-actualwidth.

Community
  • 1
  • 1
Martin
  • 5,714
  • 2
  • 21
  • 41