This topic has not yet been rated - Rate this topic

FrameworkElement.Tag Property

Gets or sets an arbitrary object value that can be used to store custom information about this object.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)
public Object Tag { get; set; }
<frameworkElement>
  <frameworkElement.Tag>
    object*
  </frameworkElement.Tag>
</frameworkElement>
<frameworkElement Tag="string"/>

XAML Values

object*

A Silverlight object or a custom object that supports object element syntax (the backing type must be nonabstract and must support a public default constructor).

string

A string value (can be a true string, or input for an object type that supports type conversion from string).

Property Value

Type: System.Object
The intended value. This property has no default value.

Dependency property identifier field: TagProperty

This property is similar to Tag properties in other programming models. Tag is intended to provide a preexisting property location where you can store some basic custom information about any FrameworkElement without requiring you to derive from FrameworkElement or an intermediate class.

Because this property takes an object, a property element usage is required to set the Tag property in XAML to anything other than an object with a type converter, such as a string. Objects used in this manner are typically not within the standard Silverlight XAML namespaces and therefore may require mapping an external namespace in order to be introduced as XAML object elements.

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Tag can be an arbitrary Object

1) If you are using the Silverlight JavaScript API, you can only use a string, but that's probably not relevant to mention here, because this is the documentation for the managed version.

2) If you want to set the Tag value to an object in the initial XAML, that is possible. However, whatever object you use has to support a XAML usage in some fashion. One possibility is to use attribute syntax along with either a binding or a resource reference, for instance:

<Button Tag="{Binding OrderBusinessObj}" .../>

or

<Button Tag="{StaticResource OrderBusinessObj}" .../>

Another possibility is to use property element syntax along with an element usage for a custom object, along with a "local" mapping to the object-defining assembly+namespace. For instance:

<Button>
  <Button.Tag>
    <local:OrderBusinessObj .../>
  </Button.Tag>
</Button>

3) If you want to set Tag in code at run-time there are no particular restrictions on which object you use. However, bear in mind that if you want to use properties of the object you set as Tag for UI purposes, using Tag might not be the best design. It might be better to associate a given object to a FrameworkElement through a Binding relationship instead, perhaps by setting the object into a single FrameworkElement's DataContext. With a Binding relationship, you can then bind particular properties of your object to properties of the FrameworkElement, using converters on the binding and/or DataTemplate techniques in order to get the right type-match and presentation for your object data in the UI.