Tecman
Tecman

Reputation: 3009

ContentControl does not show content in ControlTemplate

I need to create a custom control similar to WPF GroupBox. I started from the standard WPF Custom Control Library template in VS 2015 and defined my CustomGroupBox control like this:

public class CustomGroupBox : ContentControl
{
    static CustomGroupBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomGroupBox), new FrameworkPropertyMetadata(typeof(CustomGroupBox)));
    }
}

Then added the following minimal set of lines to develop my custom GroupBox according to the specification:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CustomGroupBox">
    <Style TargetType="{x:Type local:CustomGroupBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomGroupBox}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="Gray" BorderThickness="3" CornerRadius="3">
                        <ContentControl />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

To debug and develop, I created a test form for my custom control:

<Window x:Class="CustomGroupBoxClient.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CustomGroupBoxClient"
        xmlns:ctrl="clr-namespace:CustomGroupBox;assembly=CustomGroupBox"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ctrl:CustomGroupBox Margin="5">
            <StackPanel Orientation="Vertical">
                <TextBlock>Text Block #1</TextBlock>
                <TextBlock>Text Block #2</TextBlock>
            </StackPanel>
        </ctrl:CustomGroupBox>
    </Grid>
</Window>

However, when I launch this form, I see the border of my custom control but not the content (2 TextBlock's):

enter image description here

I've re-read many manuals and articles related to this topic, but still can't figure out why ContentControl in my ControlTemplate does not display the specified content. How to solve my problem?

Upvotes: 0

Views: 2127

Answers (1)

Try using ContentPresenter instead of ContentControl in your template:

<ContentPresenter />

By default, ContentPresenter finds the Content property of its templated parent and displays whatever it finds there. You could change the name of the property it looks for by changing its ContentSource value...

<ContentPresenter ContentSource="FooBar" />

...but since you're inheriting from ContentControl, the default is probably what you want.

Upvotes: 2

Related Questions