ContentPresenter in UserControl

I am new to WPF and I am trying to create a UserControl that will have some nested content.

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

As you can see, I want to put a StackPanel in it. When I read some articles, I have to add ContentPresenter to my UserControl, so I did it, but I can’t find what should be bound to it. Property Content.

Here is my UserControl code

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

I have tried many combinations from different articles, but I cannot find any working example of what I want to achieve.

Another question was asked earlier by a different user, but given that the answers didn’t help me: Does anyone have a simple UserControl example with one ContentPresenter?

+8
c # wpf xaml contentpresenter
source share
3 answers

I solved this problem by applying the GroupBox style. I created Syle in a ResourceDictionary that looks like this

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

And applied this style to GroupBox

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

This code works as expected

You can also refer to this wonderful article, which shows various options for achieving it: http://www.codeproject.com/Articles/82464/How-to-Embed-Arbitrary-Content-in-a-WPF-Control It It also describes why ContentPresenter not working in my code.

+9
source share

ContentPresenter is a kind of magical control. If you do not add anything to it, it will automatically set the Content , ContentTemplate and ContentTemplateSelector using TemplateBinding to TemplatedParent. That means you don’t have to supply anything, just

 <ContentPresenter/> 

in your UserControl, and it should automatically use the appropriate properties found in your UserControl. Also remember that binding like {Binding Content} always refers to your DataContext, which I think is not what you wanted.

+8
source share

You need to create a dependency property in your UserControl code, such as 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)); 

Then you need to bind to this internal content on the XAML side of this UserControl.

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

Then, when you use it instead of directly posting content to the UserControl and overwriting existing content, just add it to the InnerContent part.

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

Otherwise, using a template or style is just as good, but if you want to pack UserControl for use without forcing anyone to reference the style or template, this is probably one of your best options.

+1
source share

All Articles