You are handling the UserControl as if it were the main ContentControl (e.g. Button ), which is slightly different from what it actually is. Using Button as an example, when you add a child (i.e. A TextBlock ) to a Button that actually sets that TextBlock as a Button Content property. The way it is processed is a Button ControlTemplate , which includes a ContentPresenter to enter Content in. The visual tree ends as follows:
<Button> -start Template <Border> <ContentPresenter> -start Content <TextBlock>
So far, basically a model of your code. The problem is that instead you use a (still ContentControl derivative) UserControl , which instead of using the ControlTemplate most often defined using the XAML + code-behind model, where XAML defines the Content UserControl . (You can switch these models and a UserControl template or make the Button derived class with XAML + code, but not shared)
If you want to determine how the appearance of your UserControl in XAML is both normal and still be able to enter other content, you can add another DependencyProperty , which reflects the setting of the Content property and sets the content for it. This approach is used with derivatives of the HeaderedContentControl (i.e. Expander ), which essentially has 2 content properties, Content and Header . Using the new property will look like this:
<Border> <local:UserControl> <local:UserControl.OtherContent> <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/> </local:UserControl.OtherContent> </local:UserControl> </Border>
And then inside the UserControl XAML you need to explicitly configure the ContentPresenter bindings (you only get them for free inside the ContentControls templates):
<DockPanel> <ContentPresenter Content="{Binding Path=OtherContent, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"/> </DockPanel>
If you need ContentTemplate , ContentTemplateSelector or ContentStringFormat , you will also need to add properties and bindings for them.
source share