Hybrid UserControl
Posted: 08 Apr 2024, 08:18
I spent some time last week trying to write a subclass to UserControl which accepts XAML children - a sort of custom StackPanel which allows me to add a header and frame the contents as I like. What I want is to be able to do is something like the following
where the "Custom content" items would be displayed through a ContentPresenter (or a variant of it) appearing in the Template of the HybridControl. The motivation is to be able to write more compact XAML and reuse the same header-body layout in many places but with different types of content.
What I tried was to inherit from UserControl, to get the basic functionality of instantiating a "HybridControl.xaml" template, but I also borrowed the following from StackPanel:
I also did
as the content property metadata seemed to be the way to direct the XAML body - the "custom content" entries - to my Noesis object.
I did a lot of experimentation but only managed to get so far as to reproduce the UserControl behavior, and trying to hook anything up from within the MultiContentControl's Template to the Children property of the TemplatedParent led to a run-time crash.
Is this supposed to be possible? Is there a known way to do it?
Code: Select all
<MultiContentControl>
<TextBlock Text="Custom content 1"/>
<TextBlock Text="Custom content 2"/>
</MultiContentControl>
What I tried was to inherit from UserControl, to get the basic functionality of instantiating a "HybridControl.xaml" template, but I also borrowed the following from StackPanel:
Code: Select all
GetChildren, CreateChildrenCollection, EnsureChildrenCollection, GetLogicalChildrenCount, GetLogicalChild, OnInit
Code: Select all
NsMeta<Noesis::ContentPropertyMetaData>( "Children" );
NsProp( "Children", &MultiContentControl::GetChildren );
I did a lot of experimentation but only managed to get so far as to reproduce the UserControl behavior, and trying to hook anything up from within the MultiContentControl's Template to the Children property of the TemplatedParent led to a run-time crash.
Is this supposed to be possible? Is there a known way to do it?