Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

'Component' does not support 'Component' as content RSS

12 replies

Last post Aug 16, 2007 11:03 PM by sdaubin

(0)
  • sdaubin

    sdaubin

    Member

    50 Points

    26 Posts

    Re: 'Component' does not support 'Component' as content

    Aug 16, 2007 09:20 PM | LINK

    VladF

    Just inherit your class from the Canvas and not the Control and you will be able to both define its children in XAML and add them in code.
     

     Thanks, that works for me.

    I changed the control that contains other controls to extend Canvas.  I then had to implement my own InitializeFromXaml method:

            protected FrameworkElement InitializeFromXaml(string xaml)
            {
                FrameworkElement control = (FrameworkElement)XamlReader.Load(xaml);
                this.Children.Add(control);
                return control;
            }

     The next problem I ran into was name collision.  The xaml for my control uses named elements.  When more than one of my control is added to a canvas those named elements now conflict.  To work around this I implemented a "FindNameAndMakeUnique" method to my control which does a FindName for an element and then renames it with a unique name.  If my control xaml looks like this:

     
    <Canvas ...>

       <Rectangle x:Name="rectangle"/>

    </Canvas>

     
    The constructor of my control looks up the "rectangle" child:

    Rectangle rect = (Rectangle)FindNameAndMakeUnique("rectangle");

    After this call the name of the rectangle is something like "rectangle0".  I just have a static id that increments every time a name is made unique.  That means my version of FindName will only work once, but that's okay.
     

    Saxon
  • swirlingmass

    swirlingmass

    Participant

    1358 Points

    385 Posts

    Re: 'Component' does not support 'Component' as content

    Aug 16, 2007 10:08 PM | LINK

    sdaubin

    The next problem I ran into was name collision.  The xaml for my control uses named elements.  When more than one of my control is added to a canvas those named elements now conflict.

     You can get around this by creating a namescope for each control.  When you do XamlReader.Load, you can set the second argument to true to create a namescope.  FindName will only work in the namescope in which it is called, but that's better than your situation I think.  Here is a little more info on namescopes.

  • sdaubin

    sdaubin

    Member

    50 Points

    26 Posts

    Re: Re: 'Component' does not support 'Component' as content

    Aug 16, 2007 11:03 PM | LINK

     Thanks swirlingmass!

    Saxon