Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get and set WPF custom attached property from code behind

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.

like image 495
Dawid Jablonski Avatar asked Feb 16 '16 17:02

Dawid Jablonski


People also ask

How to create attached properties WPF?

In XAML, you set attached properties by using the syntax <attached property provider type>. <property name> , where the attached property provider is the class that defines the attached property. The following example shows how a child element of DockPanel can set the DockPanel. Dock property value.

What is code behind in WPF?

Code-behind is a term used to describe the code that is joined with markup-defined objects, when a XAML page is markup-compiled. This topic describes requirements for code-behind as well as an alternative inline code mechanism for code in XAML.

What is attached property in WPF with example?

Attached properties are properties which can be set on any wpf object (basically, at least a DependencyObject) via the DependencyObject. SetValue method. The purpose for this mechanism is to "attach" to other objects information needed by parent objects, not the child objects themselves. For example, the Grid.

What is attached property and dependency property in WPF?

An attached property is intended to be used as a type of global property that is settable on any object. In Windows Presentation Foundation (WPF), attached properties are typically defined as a specialized form of dependency property that does not have the conventional property wrapper”.


1 Answers

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

OwningClass.SetMyAttachedProperty(textBox, true);
like image 109
James Willock Avatar answered Sep 22 '22 23:09

James Willock