Adding the UIElementCollection Dependency Property to Silverlight

I want to add a dependency property to UserControl, which may contain a collection of objects UIElement. You can assume that I should get my control out Paneland use a property for this Children, but this is not suitable for my case.

I changed mine UserControlas follows:

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 use it as follows:

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

Unfortunately, I get the following error when starting the application:

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

The section Setting the property using collection syntax explicitly states that:

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

, ? UIElementCollection? , ?

+3
2

UIElementCollection Collection<UIElement> , , :

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, , .

+5

Silverlight Toolkit, System.Windows.Controls.Toolkit "ObjectCollection", XAML.

, ObjectCollection , UIElement. , IEnumerable ( ItemsSource), toolkit:ObjectCollection XAML.

ObjectCollection (Ms-PL) .

, , .

[ContentProperty], .

+1

Source: https://habr.com/ru/post/1715521/


All Articles