我是WPF的新手,我正在尝试创建一个UserControl,它将包含一些嵌套内容。
<my:InformationBox Header="General Information" Width="280">
<StackPanel>
<Label>Label1</Label>
<Label>Label2</Label>
</StackPanel>
</my:InformationBox>正如您所看到的,我想在其中放入一个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>我尝试了各种文章中的许多组合,但我找不到任何我想要实现的工作示例。
早些时候,另一个用户问了类似的问题,但给出的答案对我没有帮助:Does anyone have a simple example of a UserControl with a single ContentPresenter?
发布于 2012-07-20 16:25:26
我通过将自定义样式应用到GroupBox解决了这个问题。我在ResourceDictionary中创建了Syle,如下所示
<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-19 18:06:34
ContentPresenter是一种神奇的控件。如果您不向它提供任何内容,它将自动设置Content、ContentTemplate和ContentTemplateSelector属性,并将TemplateBinding设置为TemplatedParent。这意味着,您不需要向它提供任何东西,只需
<ContentPresenter/>它应该自动使用在您的UserControl中找到的相应属性。还要记住,像{Binding Content}这样的绑定总是引用您的DataContext,我猜这不是您想要的。
发布于 2017-06-20 02:48:54
您需要在诸如InnerContent之类的UserControl代码上创建依赖属性。
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以供使用,而不强迫任何人引用样式或模板,这可能是您更好的选择之一。
https://stackoverflow.com/questions/11558298
复制相似问题