Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight 3
 Tag Property
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Silverlight 3

Other versions are also available for the following:
.NET Framework Class Library for Silverlight
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)
Visual Basic (Declaration)
Public Property Tag As Object
    Get
    Set
Visual Basic (Usage)
Dim instance As FrameworkElement
Dim value As Object

value = instance.Tag

instance.Tag = value
C#
public Object Tag { get; set; }
XAML Property Element Usage
<frameworkElement>
  <frameworkElement.Tag>
    object*
  </frameworkElement.Tag>
</frameworkElement>
XAML Attribute Usage
<frameworkElement Tag="string"/>

XAML Values

object*

A Silverlight object or a custom object that supports object element syntax (must be nonabstract and must support a public parameterless 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 analogous 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 subclass from FrameworkElement or a derived class.

Because this property takes an object, the 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 or XAML XML namespaces and therefore may require namespace mapping to the external namespace in order to be introduced as XAML object elements.

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Tag can be an arbitrary Object      Wolf Schmidt - MSFT   |   Edit   |   Show History

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.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker