Search code examples
wpfxamlattached-properties

Set a custom Attached Property with a XAML Setter


I wanted to set a custom Attached Property that I attached to my Textboxes.

My name space is correctly imported, and used for other things in my UserControl so it works fine.

I can't seem to find the correct syntax for this.

Here is what I currently have (I tried to add brackets at different places, but it doesn't work) :

... 
  <DataTrigger ...>
    <Setter ... />
    <Setter Property="(MyCustomXMLNS:TextBoxExtend).MyProperty" Value="..." />
  </DataTrigger>
</Style.Triggers>

This is only a syntax problem here, do you guys know how to achieve this ?


Solution

  • You do not use parentheses in a Setter for an attached property, so the correct syntax is

    <Setter Property="MyCustomXMLNS:TextBoxExtend.MyProperty" Value="..."/>
    

    You would use parentheses when the property is the source of a Binding, like

    <DataTrigger Binding="{Binding Path=(MyCustomXMLNS:TextBoxExtend.MyProperty)}"
                 Value="...">