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?
c # wpf xaml contentpresenter
krzychu
source share