我是WPF的新手,我正在尝试创建一个具有一些嵌套内容的UserControl.
<my:InformationBox Header="General Information" Width="280">
<StackPanel>
<Label>Label1</Label>
<Label>Label2</Label>
</StackPanel>
</my:InformationBox>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我想将StackPanel放入其中.当我阅读一些文章时,我应该将ContentPresenter添加到我的UserControl,所以我做了,但我找不到应该绑定到它的Content属性.
这是我的UserControl代码
<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>
Run Code Online (Sandbox Code Playgroud)
我已尝试过各种文章中的许多组合,但我找不到任何我希望实现的实例.
其他用户之前曾问过类似的问题,但鉴于那里的答案对我没有帮助:有没有人有一个带有单个ContentPresenter的UserControl的简单示例?