2

I have created a UserControl and I want to expose the ItemsSource of the ItemsControl which is used in the UserControl. But I want to expose the Items through a separate property, like this:

<MyUserControl>
  <MyProperty>
    <Button>Button 1</Button>
    <Button>Button 2</Button>
  </MyProperty>
</MyUserControl>

The reason for the separation by property is because I need to create multiple properties like this. I tried creating a Dependency Property in different ways, but I am not able to get it to work as I want. Without the separate property I am able to make it work.

I found some clues in the following topic, but the solution does not use a separate property: How to create WPF usercontrol which contains placeholders for later usage

Any help is appreciated.

Community
  • 1
  • 1
Klus
  • 31
  • 4

1 Answers1

1

It turns out I needed to use two Dependency Properties to get the desired result.

Example from Microsoft which is working as expected:

Private Shared ReadOnly MyPropertyPropertyKey As DependencyPropertyKey = DependencyProperty.RegisterReadOnly("MyProperty", GetType(List(Of FrameworkElement)), GetType(MyUserControl), New FrameworkPropertyMetadata(New List(Of FrameworkElement)()))
Public Shared ReadOnly MyPropertyProperty As DependencyProperty = MyPropertyPropertyKey.DependencyProperty

Public ReadOnly Property MyProperty() As List(Of FrameworkElement)
    Get
        Return CType(GetValue(MyPropertyProperty), List(Of FrameworkElement))
    End Get
End Property
Klus
  • 31
  • 4