Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContentPresenter in UserControl

I'm new to WPF and I'm trying to create an UserControl which 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 into it. As I read some articles I am supposed to add ContentPresenter to my UserControl, so I did, but I cannot find what should be binded to it's Content property.

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've tried many combinations from various articles, but I cannot find any working example of what I want to achieve.

Similiar question was asked earlier by another user, but given there answers didn't help me: Does anyone have a simple example of a UserControl with a single ContentPresenter?

like image 530
krzychu Avatar asked Jul 19 '12 09:07

krzychu


2 Answers

ContentPresenter is kind of a magic control. If you don't supply anything to it, it will automatically set the Content, ContentTemplate and ContentTemplateSelector property with a TemplateBinding to the TemplatedParent. Which means, you don't need to supply anything to it, just

<ContentPresenter/>

in your UserControl, and it should automatically use the corresponding properties found in your UserControl. Also remember that a binding like {Binding Content} always referes to your DataContext, which i guess is not what you wanted.

like image 100
dowhilefor Avatar answered Oct 19 '22 02:10

dowhilefor


You need to create a dependency property on your UserControl code behind 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 that InnerContent on the XAML side of that UserControl.

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

Then when you use it instead of placing content in the UserControl directly and overwriting the existing content just add it to the InnerContent portion.

    <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're wanting to package up a UserControl for use without forcing anyone to also reference a style or template this is probably one of your better options.

like image 34
Michael Puckett II Avatar answered Oct 19 '22 01:10

Michael Puckett II