是的,在你的UserControl
的XAML聲明ContentControl
。
使其在UserControl
的代碼隱藏中將其Content
屬性綁定到DependencyProperty
。
在您的UserControl類的頂部添加屬性:[ContentProperty("Name_Of_Your_Dependency_Property")]
。
然後你可以像你在你的問題那樣精確地做。該屬性定義了默認的依賴項屬性,因此您不必指定<custom:myControl1.MyDP>
。
喜歡的東西:
[ContentProperty("InnerContent")]
public class MyControl : UserControl
{
#region InnerContent
public FrameworkElement InnerContent
{
get { return (FrameworkElement)GetValue(InnerContentProperty); }
set { SetValue(InnerContentProperty, value); }
}
// Using a DependencyProperty as the backing store for InnerContent. This enables animation, styling, binding, etc...
public static readonly DependencyProperty InnerContentProperty =
DependencyProperty.Register("InnerContent", typeof(FrameworkElement), typeof(MyControl), new UIPropertyMetadata(null));
#endregion
}
<UserControl ...>
<ContentControl Content="{Binding InnerContent, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
</UserControl>
如果能控制從繼承你做的一樣?由於某種原因,我無法完成工作? – 2014-07-22 21:13:43