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)
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.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
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.
- 10/14/2008
- Wolf Schmidt - MSFT
- 2/19/2009
- Wolf Schmidt - MSFT