您将需要向控件添加DependencyProperty.如果从UserControl或Control派生,则xaml看起来会略有不同.
public partial class MyControl : UserControl { public MyControl() { InitializeComponent(); } public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(MyControl), new UIPropertyMetadata(null)); public DataTemplate ItemTemplate { get { return (DataTemplate) GetValue(ItemTemplateProperty); } set { SetValue(ItemTemplateProperty, value); } } }
这是用于UserControl的xaml.
这是控件的xaml: