Search code examples
c#.netwpfxamlattached-properties

Attached property declaration


I have created an attached property to be added to UserControls. This attached property requires a binding, and this binding requires a converter.

As the resources are set after the UserControl declaration I'm looking a way to declare the attached property after the resource creation. How can I do that?

An example, if I define a background as a static resource I cannot set the background on the control creation but after the resources creation:

<UserControl ...
             ...
             ...>

<UserControl.Resources>
    background color declared
</UserControl.Resrouces>

<UserControl.Background>
    usage of the StaticResource here is valid.
</UserControl.Background>

So I want to the same with an attached property that I woudl normaly define as:

<UserControl xx:MyAttachedProperty.Bla="{Binding A}" >

But as I need a converter I want to specify it after the resources.

Hope it's clear. Thanks.


Solution

  • You can define yourConverter as a resource one hierarchy up either as a part of Window or Appand than you can use it just like you intended to.

    Moreover moving common resources up to App level give you advantage of re-usability which different User controls can share. Move your converter to App.xaml -

    <App.Resources>
      <!-- Your converter here -->
    </App.Resources>