WPF - ContentPresenter 不会显示内容

WPF - ContentPresenter won't display Content

我正在尝试创建一个 container-like 组件。它使用 MaterialDesign Card 作为容器,在其中放置标题并允许 space 用于内容。

<ContentControl
    x:Class="Client.Components.AisCard"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="450"
    d:DesignWidth="800">


    <materialDesign:Card
        Margin="0,0,20,0"
        Padding="20,20,20,30">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <TextBlock
                Grid.Row="0"
                Text="Opmerkingen"
                Style="{StaticResource MaterialDesignHeadline5TextBlock}"
                FontWeight="Medium"
                Margin="0,0,0,10" />

            <ContentPresenter Grid.Row="1" />
        </Grid>
    </materialDesign:Card>

</ContentControl>

然后,我在视图中调用组件并尝试填充其内容:

<Components:AisCard
    Grid.Column="0"
    Grid.Row="0">
    <TextBlock Text="Hello World" />
</Components:AisCard>

结果如下所示:

我希望它看起来像这样:

也许我误解了 ContentPresenter 的工作方式。也许在组件内部使用组件不是可行的方法。有人可以就此事提供某种形式的帮助吗?

TextBlock 取代了 Card,因为两者都只是设置了内容 属性。您必须将 Card 声明为 ControlTemplate 的一部分:

<ContentControl
    x:Class="Client.Components.AisCard"
    ...>
    <ContentControl.Template>
        <ControlTemplate TargetType="ContentControl">
            <materialDesign:Card
                Margin="0,0,20,0"
                Padding="20,20,20,30">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <TextBlock
                        Grid.Row="0"
                        Text="Opmerkingen"
                        Style="{StaticResource MaterialDesignHeadline5TextBlock}"
                        FontWeight="Medium"
                        Margin="0,0,0,10" />

                    <ContentPresenter Grid.Row="1" />
                </Grid>
            </materialDesign:Card>
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>