Xaml 如何在UserControl内部填充StackPanel?

Xaml 如何在UserControl内部填充StackPanel?,xaml,uwp,Xaml,Uwp,我正在创建一个定制面板,它基本上只是一个具有一些附加功能的爱好者StackPanel。我打算使用一个UserControl,其中包含一个StackPanel,但我不知道如何让我的UserControl接受内容来填充它的StackPanel 这就是我正在尝试的: <UserControl x:Class="transformations.fold_panel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presen

我正在创建一个定制面板,它基本上只是一个具有一些附加功能的爱好者
StackPanel
。我打算使用一个
UserControl
,其中包含一个
StackPanel
,但我不知道如何让我的
UserControl
接受内容来填充它的
StackPanel

这就是我正在尝试的:

<UserControl
    x:Class="transformations.fold_panel"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:transformations"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel Orientation="Vertical">
        <Button Content="First" />
        <ContentControl Content="{x:Bind Content, Mode=OneWay}"></ContentControl>
    </StackPanel>
</UserControl>

用法:

<local:fold_panel>
     <Button Content="Second" />
</local:fold_panel>

当我尝试此操作时,会出现以下错误:
WinRT-origing-error-0x80070057:“参数不正确”。

您不能将
UserControl
堆栈面板的
内容
绑定到同一
UserControl
内容
属性。这将引入循环引用

在您的示例中,
fold_面板的
Content
属性将设置为您在XAML标记中定义的
StackPanel

如果您希望能够在
StackPanel
中设置
ContentControl
Content
,则应向
fold\u panel
类添加一个,并将
ContentControl
Content
属性绑定到此属性:

<ContentControl Content="{x:Bind CustomContent, Mode=OneWay}" />

然后可以设置自定义属性,如下所示:

<local:fold_panel>
    <local:fold_panel.CustomContent>
        <Button Content="Second" />
    <local:fold_panel.CustomContent>
</local:fold_panel>

但是,如果您确实需要自定义的
StackPanel
,则应该创建一个继承自
StackPanel
的类,而不是
UserControl