10

我是 WPF 新手,我正在尝试创建一个包含一些嵌套内容的 UserControl。

<my:InformationBox Header="General Information" Width="280">
    <StackPanel>
        <Label>Label1</Label>
        <Label>Label2</Label>
    </StackPanel>
</my:InformationBox>

如您所见,我想将 StackPanel 放入其中。当我阅读一些文章时,我应该将 ContentPresenter 添加到我的 UserControl,所以我这样做了,但我找不到应该绑定到它的 Content 属性的内容。

这是我的用户控件代码

<UserControl x:Class="ITMAN.InformationBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="200" d:DesignWidth="280" Name="infoBox" Loaded="infoBox_Loaded">
    <StackPanel Width="{Binding ElementName=infoBox, Path=Width}" HorizontalAlignment="Stretch">
        <Label Content="{Binding ElementName=infoBox, Path=Header}" />
        <Border BorderThickness="0,1,0,0" Padding="10 5" Margin="5 0 5 10" BorderBrush="#B4CEDE">
            <StackPanel>
                <ContentPresenter Content="{Binding Content}" />
                <Label Content="End" />
            </StackPanel>
        </Border>
    </StackPanel>
</UserControl>

我从各种文章中尝试了许多组合,但我找不到任何我想要实现的工作示例。

另一个用户早些时候提出了类似的问题,但给出的答案对我没有帮助:有没有人有一个带有单个 ContentPresenter 的 UserControl 的简单示例?

4

3 回答 3

10

ContentPresenter是一种魔法控制。如果您不向它提供任何东西,它会自动将Content,ContentTemplateContentTemplateSelector属性设置TemplateBinding为 TemplatedParent。这意味着,您不需要向它提供任何东西,只需

<ContentPresenter/>

在您的 UserControl 中,它应该自动使用在您的 UserControl 中找到的相应属性。还要记住,绑定{Binding Content}总是引用您的 DataContext,我猜这不是您想要的。

于 2012-07-19T10:06:33.777 回答
10

我通过将自定义样式应用于GroupBox. 我在 中创建了 Syle ResourceDictionary,如下所示

<Style x:Key="InformationBoxStyle" TargetType="GroupBox">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="GroupBox">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Label>
                        <ContentPresenter Margin="4" ContentSource="Header"
                               RecognizesAccessKey="True" />
                    </Label>
                    <Border Grid.Row="1" BorderThickness="0,1,0,0" Padding="10 5"
                               Margin="5 0 5 10" BorderBrush="#B4CEDE">
                        <StackPanel>
                            <ContentPresenter />
                        </StackPanel>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

并将这种风格应用于GroupBox

<GroupBox Header="General Information" Width="280" Style="{StaticResource InformationBoxStyle}">
    <StackPanel>
        <Label>Label1</Label>
        <Label>Label2</Label>
    </StackPanel>
</GroupBox>

此代码按预期工作

您也可以参考这篇很棒的文章,其中显示了实现它的不同选项:http: //www.codeproject.com/Articles/82464/How-to-Embed-Arbitrary-Content-in-a-WPF-Control 它也描述了为什么ContentPresenter在我的代码中不起作用。

于 2012-07-20T08:25:26.130 回答
2

您需要在您的 UserControl 代码上创建一个依赖属性,例如 InnerContent。

    public object InnerContent
    {
        get { return GetValue(InnerContentProperty); }
        set { SetValue(InnerContentProperty, value); }
    }

    public static readonly DependencyProperty InnerContentProperty =
        DependencyProperty.Register("InnerContent", typeof(object), typeof(ConfirmationControl), new PropertyMetadata(null));

然后,您需要在该 UserControl 的 XAML 端绑定到该 InnerContent。

 <ContentControl Content="{Binding InnerContent, ElementName=userControl}" />

然后,当您使用它而不是直接在 UserControl 中放置内容并覆盖现有内容时,只需将其添加到 InnerContent 部分。

    <UserControls:InformationBox>
        <UserControls:InformationBox.InnerContent>
            <TextBlock Text="I'm in the InnerContent" />
        </UserControls:InformationBox.InnerContent>
    </UserControls:InformationBox>

否则,使用模板或样式也一样好,但如果您想打包 UserControl 以供使用而不强迫任何人也引用样式或模板,这可能是您更好的选择之一。

于 2017-06-19T18:48:54.053 回答