在 Silverlight 中添加 UIElementCollection DependencyProperty

发布于 2024-08-02 03:04:55 字数 1479 浏览 2 评论 0原文

我想向可以包含 UIElement 对象集合的 UserControl 添加依赖属性。 您可能建议我应该从 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'.

使用集合语法设置属性部分明确指出:

[...]您无法在 XAML 中显式指定 [UIElementCollection],因为 UIElementCollection 不是可构造的类。

我可以做什么来解决我的问题? 解决方案是否只是使用另一个集合类而不是 UIElementCollection? 如果是,建议使用什么集合类?

I want to add a dependency property to a UserControl that can contain a collection of UIElement objects. You might suggest that I should derive my control from Panel and use the Children property for that, but it is not a suitable solution in my case.

I have modified my UserControl like this:

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);
    }
  }

}

and I'm using it like this:

<local:SilverlightControl1>
  <local:SilverlightControl1.Controls>
    <Button Content="A"/>
    <Button Content="B"/>
  </local:SilverlightControl1.Controls>
</local:SilverlightControl1>

Unfortunately I get the following error when I run the application:

Object of type 'System.Windows.Controls.Button' cannot be converted to type
'System.Windows.Controls.UIElementCollection'.

In the Setting a Property by Using a Collection Syntax section it is explicitly stated that:

[...] you cannot specify [UIElementCollection] explicitly in XAML, because UIElementCollection is not a constructible class.

What can I do to solve my problem? Is the solution simply to use another collection class instead of UIElementCollection? If yes, what is the recommended collection class to use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜光 2024-08-09 03:04:55

我将属性的类型从 UIElementCollection 更改为 Collection ,这似乎解决了问题:

public partial class SilverlightControl1 : UserControl {

  public static readonly DependencyProperty ControlsProperty
    = DependencyProperty.Register(
      "Controls",
      typeof(Collection<UIElement>),
      typeof(SilverlightControl1),
      new PropertyMetadata(new Collection<UIElement>())
    );

  public Collection<UIElement> Controls {
    get {
      return (Collection<UIElement>) GetValue(ControlsProperty);
    }
  }

}

在 WPF 中 UIElementCollection 有一些功能导航逻辑和视觉树,但这在 Silverlight 中似乎不存在。 在 Silverlight 中使用另一种集合类型似乎不会造成任何问题。

I changed the type of my property from UIElementCollection to Collection<UIElement> and that seems to solve the problem:

public partial class SilverlightControl1 : UserControl {

  public static readonly DependencyProperty ControlsProperty
    = DependencyProperty.Register(
      "Controls",
      typeof(Collection<UIElement>),
      typeof(SilverlightControl1),
      new PropertyMetadata(new Collection<UIElement>())
    );

  public Collection<UIElement> Controls {
    get {
      return (Collection<UIElement>) GetValue(ControlsProperty);
    }
  }

}

In WPF UIElementCollection has some functionality to navigate the logical and visual tree, but that seems to be absent in Silverlight. Using another collection type in Silverlight doesn't seem to pose any problem.

如梦亦如幻 2024-08-09 03:04:55

如果您使用的是 Silverlight Toolkit,则 System.Windows.Controls.Toolkit 程序集包含一个“ObjectCollection”其设计目的是使这种事情在 XAML 中更容易完成。

这确实意味着您的属性需要是 ObjectCollection 类型才能工作,因此您将失去 UIElement 的强类型。 或者,如果它是 IEnumerable 类型(像大多数 ItemsSource 一样),您可以在 XAML 中显式定义 toolkit:ObjectCollection 对象。

考虑使用它,或者简单地借用ObjectCollection 的源 (Ms-PL) 并在您的项目中使用它。

可能有一种方法可以让解析器在集合场景中实际工作,但这感觉更容易一些。

我还建议添加 [ContentProperty] 属性,以便设计时体验更加清晰。

If you're using the Silverlight Toolkit, the System.Windows.Controls.Toolkit assembly contains an "ObjectCollection" which was designed to make this kind of thing easier to do in XAML.

It does mean that your property would need to be of type ObjectCollection to work, so you lose your strong typing to UIElement. Alternatively, if it is an IEnumerable type (like most ItemsSource), you could explicitly define the toolkit:ObjectCollection object in XAML.

Consider using that, or simply borrowing the source to ObjectCollection (Ms-PL) and using it in your project.

There may be a way to get the parser to actually work in a collection scenario, but this feels a little easier.

I'd also recommend adding a [ContentProperty] attribute so that the design-time experience is a little cleaner.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文