【发布时间】:2009-08-19 11:56:23
【问题描述】:
我想向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'.
在Setting a Property by Using a Collection Syntax 部分中明确指出:
[...] 您不能在 XAML 中显式指定 [UIElementCollection],因为 UIElementCollection 不是可构造的类。
我可以做些什么来解决我的问题?解决方案是否只是使用另一个集合类而不是UIElementCollection?如果是,推荐使用什么集合类?
【问题讨论】:
标签: c# silverlight xaml silverlight-3.0 dependency-properties