Here is an example of one way to do what you want:
Firstly, the code is UserControl1.xaml.cs
public partial class UserControl1 : UserControl { public static readonly DependencyProperty MyContentProperty = DependencyProperty.Register("MyContent", typeof(object), typeof(UserControl1)); public UserControl1() { InitializeComponent(); } public object MyContent { get { return GetValue(MyContentProperty); } set { SetValue(MyContentProperty, value); } } }
And the XAML user control is UserControl1.xaml
<UserControl x:Class="InCtrl.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="300" Name="MyCtrl"> <StackPanel> <Button Content="Up"/> <ContentPresenter Content="{Binding ElementName=MyCtrl, Path=MyContent}"/> <Button Content="Down"/> </StackPanel> </UserControl>
Finally, xaml uses our wonderful new control:
<Window x:Class="InCtrl.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:me="clr-namespace:InCtrl" Title="Window1" Height="300" Width="300"> <Grid> <me:UserControl1> <me:UserControl1.MyContent> <Button Content="Middle"/> </me:UserControl1.MyContent> </me:UserControl1> </Grid> </Window>
Nir
source share