C# ContentControl';s的ContentPresenter为空?

C# ContentControl';s的ContentPresenter为空?,c#,wpf,xaml,C#,Wpf,Xaml,在XAML中,我试图创建一个非常简单的“内容容器”,其中包含一个随机元素(在本例中,它是一个TextBlock),但以下内容只是空白,不包含指定的TextBlock元素 ContentControl是否是此场景中要使用的正确元素 <ContentControl> <ContentControl.Content> <TextBlock Text="Hello" /> </ContentControl.Content>

在XAML中,我试图创建一个非常简单的“内容容器”,其中包含一个随机元素(在本例中,它是一个
TextBlock
),但以下内容只是空白,不包含指定的
TextBlock
元素

ContentControl
是否是此场景中要使用的正确元素

<ContentControl>
    <ContentControl.Content>
        <TextBlock Text="Hello" />
    </ContentControl.Content>
    <ContentControl.Template>
        <ControlTemplate>
            <Border Background="Red">
                <ContentPresenter />
            </Border>
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>

只需在ControlTemplate上设置
TargetType=“ContentControl”

<ContentControl>
    <ContentControl.Content>
        <TextBlock Text="Hello" />
    </ContentControl.Content>
    <ContentControl.Template>
        <ControlTemplate TargetType="ContentControl"> <!-- here -->
            <Border Background="Red">
                <ContentPresenter />
            </Border>
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>


要使其可重用,可以声明如下内容控件样式:

<Style TargetType="ContentControl" x:Key="RedBorderContentControlStyle">
    <Setter Property="Background" Value="Red"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <ContentPresenter
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<ContentControl Style="{StaticResource RedBorderContentControlStyle}">
    <TextBlock Text="Hello"/>
</ContentControl>

然后像这样使用它:

<Style TargetType="ContentControl" x:Key="RedBorderContentControlStyle">
    <Setter Property="Background" Value="Red"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <ContentPresenter
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<ContentControl Style="{StaticResource RedBorderContentControlStyle}">
    <TextBlock Text="Hello"/>
</ContentControl>

@Clemens我有一个
静态资源
,可以是任何东西。我想在一个红色的边框内展示任何东西。