に、UIElement
オブジェクトのコレクションを含むことができる依存プロパティを追加したいとします。私はPanel
から私のコントロールを派生させ、それにはChildren
のプロパティを使うべきだと示唆しているかもしれませんが、私の場合は適切な解決策ではありません。SilverlightでのUIElementCollection DependencyPropertyの追加
私はこのような私の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'.
Setting a Property by Using a Collection Syntaxセクションでは、次のように明示的に記載されています。
[...] UIElementCollectionは構成可能なクラスではないため、[UIElementCollection]をXAMLで明示的に指定することはできません。
問題を解決するにはどうすればよいですか?ソリューションは単にUIElementCollection
の代わりに別のコレクションクラスを使用するだけですか?はいの場合は、使用する推奨コレクションクラスは何ですか?
さらに簡単に - うまくいきます。私が試してみたところでは、最初のプロパティのメタデータをコレクションのインスタンスに設定するのを忘れていました。 –