FrameworkElement..::.Tag Property Home
This page is specific to:Microsoft Version:3.03.5Silverlight 34.0
.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)
Syntax

'Usage

Dim instance As FrameworkElement
Dim value As Object

value = instance.Tag

instance.Tag = value

'Declaration

Public Property Tag As Object
    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 (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.
Remarks

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.

Platforms

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

See Also

Reference

Other Resources

Community Content

Tag can be an arbitrary Object
Added by:Wolf Schmidt - MSFT

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.

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View