wpf- contentpresenter赢得了显示内容

发布于 2025-01-17 18:12:32 字数 1714 浏览 3 评论 0原文

我正在尝试创建一个类似容器的组件。它使用MaterialDesign卡作为容器,将标题放入其中并允许内容空间。

<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的工作方式。也许在组件内使用组件不是要走的方法。有人可以在此事上提供某种形式的帮助吗?

I am trying to create a container-like component. It uses the MaterialDesign Card as a container, places a title in it and allows space for Content.

<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>

Then, I call the Component in a view and attempt to fill its Content:

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

The result looks like this:

enter image description here

I expected it to look like this:

enter image description here

Maybe I have misunderstood the way that a ContentPresenter works. Maybe using a Component inside of a Component is not the way to go. Could anyone offer some form of assistance in the matter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光病人 2025-01-24 18:12:32

TextBlock 取代了 Card,因为两者都只是设置 Content 属性。您必须将卡声明为 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>

The TextBlock replaces the Card, because both just set the Content property. You would have to declare the Card as part of a 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文