UserControl
オブジェクトのコレクションを含むことができる依存関係プロパティを に追加したいと考えていUIElement
ます。コントロールを派生させてそのプロパティをPanel
使用する必要があると提案するかもしれませんChildren
が、私の場合、それは適切な解決策ではありません。
私はUserControl
このように変更しました:
public partial class SilverlightControl1 : UserControl {
public static readonly DependencyProperty ControlsProperty
= DependencyProperty.Register(
"Controls",
typeof(UIElementCollection),
typeof(SilverlightControl1),
null
);
public UIElementCollection Controls {
get {
return (UIElementCollection) GetValue(ControlsProperty);
}
set {
SetValue(ControlsProperty, value);
}
}
}
そして、私は次のように使用しています:
<local:SilverlightControl1>
<local:SilverlightControl1.Controls>
<Button Content="A"/>
<Button Content="B"/>
</local:SilverlightControl1.Controls>
</local:SilverlightControl1>
残念ながら、アプリケーションを実行すると次のエラーが発生します。
Object of type 'System.Windows.Controls.Button' cannot be converted to type
'System.Windows.Controls.UIElementCollection'.
コレクション構文を使用してプロパティを設定するセクションでは、次のように明示的に述べられています。
[...] UIElementCollection は構築可能なクラスではないため、[UIElementCollection] を XAML で明示的に指定することはできません。
問題を解決するにはどうすればよいですか? 代わりに別のコレクションクラスを使用するだけで解決できUIElementCollection
ますか? はいの場合、使用する推奨のコレクション クラスは何ですか?