Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight
 Tag Property
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
Visual Basic (Usage)
Dim instance As FrameworkElement
Dim value As Object

value = instance.Tag

instance.Tag = value
C#
public Object Tag { get; set; }
XAML Attribute Usage
<object Tag="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 pre-existing property location where you can store some basic custom information about any FrameworkElement without requiring you to subclass from FrameworkElement or a derived class.

Silverlight 2 restricts setting the Tag value to a string, despite the Object type in the signature. This restriction is enforced both by the XAML processor when parsing property values for Tag, and by the native code implementation if you attempt to set Tag to an object in code.

For information on the operating systems and browsers supported by Silverlight, see "Supported Operating Systems and Browsers" in Silverlight Architecture.

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   |  

The Remarks in this topic have some doc bugs, which will be fixed in an upcoming release.

One of the Remarks states that Silverlight always processes Tag as a string. This is not entirely correct.

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 "my" mapping to the object-defining assembly+namespace. For instance:

<Button>
  <Button.Tag>
    <my: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
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker