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.
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
Member
50 Points
26 Posts
Re: 'Component' does not support 'Component' as content
Aug 16, 2007 09:20 PM | LINK
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.
swirlingmass
Participant
1358 Points
385 Posts
Re: 'Component' does not support 'Component' as content
Aug 16, 2007 10:08 PM | LINK
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
Member
50 Points
26 Posts
Re: Re: 'Component' does not support 'Component' as content
Aug 16, 2007 11:03 PM | LINK
Thanks swirlingmass!