Get and set WPF custom attached property from code behind
Asked Answered
G

1

10

I generate objects of framework Rectangle class (which is sealed) and programmatically add them to Canvas. I want to add some information to that objects. Is there such a possibility? I can get and set attached properties in XAML but these is not useful for me.

Gummy answered 16/2, 2016 at 17:8 Comment(3)
Take a look at Custom Attached Properties.Lacteous
@ Clemens, if I wrote that I can get and set this property in XAML it means that I can properly register it.Gummy
The section from Clemens link that describes how to do this in code is at: Section "Attached Properties in Code". This is similar to the accepted answer, with additional explanation.Stormystorting
T
22

You can set attached properties in code, something like this:

OwningClass.SetMyAttachedProperty(textBox, true);
Twelvemonth answered 16/2, 2016 at 17:14 Comment(1)
It's good for any "attached property", presuming you declare it in the standard way. Here's an example of me using a custom one in code: github.com/ButchersBoy/MaterialDesignInXamlToolkit/blob/master/…Twelvemonth

© 2022 - 2024 — McMap. All rights reserved.