WPF用户控件中的多个内容演示者

我正在创建一个WPF用户控件,它就像一个窗口,其中大部分布局已经设置。 但是我希望用户放置控件的部分很少。 为了实现这一点,我相信我需要在用户控件中公开一些依赖属性。

输出应该有点像这样

在此处输入图像描述

用户控制代码

public class Class1 : UserControl { public ContentControl Content1 { get { return (ContentControl)GetValue(Content1Property); } set { SetValue(Content1Property, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty Content1Property = DependencyProperty.Register("Content1", typeof(ContentControl), typeof(Class1), null); public ContentControl Content2 { get { return (ContentControl)GetValue(Content2Property); } set { SetValue(Content2Property, value); } } // Using a DependencyProperty as the backing store for Content2. This enables animation, styling, binding, etc... public static readonly DependencyProperty Content2Property = DependencyProperty.Register("Content2", typeof(ContentControl), typeof(Class1), null); public ContentControl Content3 { get { return (ContentControl)GetValue(Content3Property); } set { SetValue(Content3Property, value); } } // Using a DependencyProperty as the backing store for Content3. This enables animation, styling, binding, etc... public static readonly DependencyProperty Content3Property = DependencyProperty.Register("Content3", typeof(ContentControl), typeof(Class1),null); } 

并且控件的相应xaml是

                                  

我试图使用它有点像这样

             

上面代码的输出都没有。

在此处输入图像描述

Class1.cs更改:

 public class Class1 : Control { public Class1() { this.DefaultStyleKey = typeof(Class1); } public object Content1 { get { return GetValue(Content1Property); } set { SetValue(Content1Property, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty Content1Property = DependencyProperty.Register("Content1", typeof(object), typeof(Class1), null); public object Content2 { get { return GetValue(Content2Property); } set { SetValue(Content2Property, value); } } // Using a DependencyProperty as the backing store for Content2. This enables animation, styling, binding, etc... public static readonly DependencyProperty Content2Property = DependencyProperty.Register("Content2", typeof(object), typeof(Class1), null); public object Content3 { get { return GetValue(Content3Property); } set { SetValue(Content3Property, value); } } // Using a DependencyProperty as the backing store for Content3. This enables animation, styling, binding, etc... public static readonly DependencyProperty Content3Property = DependencyProperty.Register("Content3", typeof(object), typeof(Class1), null); } 

样式更改(Dictionary1.xaml):

     

MainWindow.xaml:

                 

附加字典(App.xaml):

          

在此处输入图像描述

这对我有用。