Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom attributes to an element in XAML?

Tags:

In html, there is nothing preventing you from creating custom attributes, since it is effectively xml such as

<span myProperty="myValue"></span> 

Then you can read that property via javascript.

Can you do the same thing in wpf? For example:

<Canvas MyProperty="MyValue" Name="MyCanvas" DataContext="{Binding}" Background="Black" Margin="181,0,0,0"></Canvas> 

and If so how would you access that property? For example:

MyCanvas.MyProperty; 
like image 408
ohmusama Avatar asked Apr 25 '11 20:04

ohmusama


People also ask

How do I add custom attributes to my framework?

AttributeUsage has a named parameter, AllowMultiple , with which you can make a custom attribute single-use or multiuse. In the following code example, a multiuse attribute is created. In the following code example, multiple attributes of the same type are applied to a class. [Author("P.

How do I create a custom control in XAML?

To create a user control, select User Control from the Add New Item dialog, as shown in Figure 2. This XAML renders my earlier prototype and shows just how simple a user control can be. Of course, there's no custom behavior yet, only the built-in behavior of the controls I declare.

How do I create a custom attribute in .NET core?

Declaring Custom AttributesWe can define an attribute by creating a class. This class should inherit from the Attribute class. Microsoft recommends appending the 'Attribute' suffix to the end of the class's name. After that, each property of our derived class will be a parameter of the desired data type.


1 Answers

The closest you can get are attached properties. Basically, another class defines a known property (i.e. MyProperty), which can be set on other elements.

An example would be the Canvas.Left property, which is used by the Canvas to position a child element. But any class can define an attached property.

Attached properties are the key behind attached behaviors, which is a great feature of WPF/Silverlight.

EDIT:

Here is an example class:

namespace MyNamespace {     public static class MyClass {          public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.RegisterAttached("MyProperty",             typeof(string), typeof(MyClass), new FrameworkPropertyMetadata(null));          public static string GetMyProperty(UIElement element) {             if (element == null)                 throw new ArgumentNullException("element");             return (string)element.GetValue(MyPropertyProperty);         }         public static void SetMyProperty(UIElement element, string value) {             if (element == null)                 throw new ArgumentNullException("element");             element.SetValue(MyPropertyProperty, value);         }     } } 

Then in XAML you can use it like so:

xmlns:local="clr-namespace:MyNamespace"  <Canvas local:MyClass.MyProperty="MyValue" ... /> 

You can get the property from code using MyClass.GetMyProperty and passing in the element on which the property is set.

like image 129
CodeNaked Avatar answered Sep 19 '22 22:09

CodeNaked