我已經創建了兩個自定義控件。 1. LeafControl 2- LeafItemControl。 在LeafControl中,我創建了一個類型爲「List」的Dependency屬性作爲「Items」。自定義控件依賴項屬性集合,統計嵌套控件項目
另外在LeafItemControl中,我公開了一個類型爲ContentControl的更多依賴項屬性調用「ItemDetails」。
<!---Base Custom Control "LeafControl"-->
<uc:LeafControlItem.ItemDetails> <!--"ItemDetails" is a Dependency Property of type "LeafControl" in "LeafControlItem" custom control --> <uc:LeafControl> <!--Nested Control of same type ???--> <uc:LeafControl.Items> <uc:LeafControlItem Level="Some Type"> <uc:LeafControlItem.ItemContent> <GroupBox BorderThickness="0"> <StackPanel Orientation="Horizontal"> <TextBox Text="Property"></TextBox> </StackPanel> </GroupBox> </uc:LeafControlItem.ItemContent> </uc:LeafControlItem> <uc:LeafControlItem Level="Variable"> <uc:LeafControlItem.ItemContent> <GroupBox BorderThickness="0"> <StackPanel Orientation="Horizontal"> <TextBox Text="Ellipse2.Top"></TextBox> </StackPanel> </GroupBox> </uc:LeafControlItem.ItemContent> </uc:LeafControlItem> </uc:LeafControl.Items> </uc:LeafControl> </uc:LeafControlItem.ItemDetails> </uc:LeafControlItem>
當我嘗試訪問基本自定義控件中的「項目」時。所有的孩子自定義控件都增加了爲什麼?我應該怎麼做,以便每個自定義控件對象(基礎和兒童)都有單獨的「項目」。
我已經使用依賴項屬性在基本自定義控件這樣的:
#region LeafControlItemCollection
public List<LeafControlItem> Items
{
get { return (List<LeafControlItem>)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
public static readonly DependencyProperty ItemsProperty =
DependencyProperty.Register(
"Items", typeof(List<LeafControlItem>), typeof(LeafControl),
new FrameworkPropertyMetadata(new List<LeafControlItem>(), null, null)
);
#endregion
請建議我在哪裏做錯了。
我的答案適合您嗎? –
嗨,謝謝!那工作。我還發現了另一種方法。將「Items」依賴項屬性數據類型暴露爲「ItemsControl」。在xaml中,我可以像其他itemscontrol一樣將Items添加到它。它也可以正常工作。 – user2565258