[英]XAML container control for specific controls
我正在嘗試創建一個自定義控件,該控件具有特定 class 的多個子級。 例如,WPF 中的 ToolBarTray 只能包含 ToolBar 子項。
<ToolBarTray>
<ToolBar>..</ToolBar>
<ToolBar>..</ToolBar>
</ToolBarTray>
這種對兒童類型的約束由 XAML 語法強制執行。 如何使用兩個自定義控件實現相同的結果:
<MyCustomControlA>
<MyCustomControlB>..</MyCustomControlB>
<MyCustomControlB>..</MyCustomControlB>
</MyCustomControlA>
我試圖創建一個擴展 ItemsControl 的自定義控件,但無法在 XAML 語法中強制執行任何約束。
那就是ToolBarTray
class 使用的:
[ContentProperty("ToolBars")]
public class ToolBarTray : FrameworkElement, IAddChild
...
ToolBars
屬性的類型是Collection<ToolBar>
。
您可以使用ContentPropertyAttribute
並按如下方式實現您的自定義控件:
public class MyCustomControlB : Control
{
}
[ContentProperty("MyCustomControls")]
public class MyCustomControlA : FrameworkElement
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public System.Collections.ObjectModel.Collection<MyCustomControlB> MyCustomControls { get; }
.
.
.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.