I am trying to implement my own control, which can host one or more children of the same type as the control itself:
<local:MyControl x:Name="control1">
<local:MyControl x:Name="control2">
<local:MyControl x:Name="control3">
<local:MyControl x:Name="control4">
<local:MyControl x:Name="control5">
<local:MyControl x:Name="control6">
<local:MyControl x:Name="control7"/>
</local:MyControl>
</local:MyControl>
</local:MyControl>
</local:MyControl>
</local:MyControl>
</local:MyControl>
The problem that I have is that only the first instance is known as root
and the others are children of the first one. And they may even be children of each other.
Here the code for MyControl class:
[ContentProperty("MyChildren")]
class MyControl : ItemsControl
{
public List<MyControl> MyChildren
{
get { return (List<MyControl>)GetValue(MyChildrenProperty); }
set { SetValue(MyChildrenProperty, value); }
}
// Using a DependencyProperty as the backing store for Children. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyChildrenProperty =
DependencyProperty.Register("MyChildren", typeof(List<MyControl>), typeof(MyControl), new UIPropertyMetadata(new List<MyControl>()));
}
This code is what I have tried, but this isn't working.
How do I implement a custom control that can host child controls of the same type, in which it should be possible to do things like in the example above?