Condividi tramite


FrameworkElement.Style Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets an instance Style that is applied for this object during rendering.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Property Style As Style
public Style Style { get; set; }
<frameworkElement Style="{StaticResource styleResourceKey}"/>

XAML Values

  • styleResourceKey
    The key that identifies the style being requested. The key refers to an existing resource in a ResourceDictionary.
NoteNote:

XAML property element syntax to define an inline style is technically possible, but not recommended for setting FrameworkElement.Style. This is because any property set through a single-use inline style could just as easily have been set by directly setting the property as a XAML attribute (or property element). Defining a one-use style to set that same property is generally poor markup style and can cause a confusing run-time property setting behavior.

Property Value

Type: System.Windows.Style
The applied style for the object, if present; otherwise, nulla null reference (Nothing in Visual Basic). The default for a default-constructed FrameworkElement is nulla null reference (Nothing in Visual Basic).

Remarks

Dependency property identifier field: StyleProperty

Setting the Style property directly is not the most common way to style a UI element's appearance in Silverlight. Instead, it is more common to influence a UI element's appearance through one of the following techniques:

  • Control authors write a control that is templatable. Applications that use the control can change its appearance by providing a new ControlTemplate.

  • Control authors define a default style that is applied to all instances of the control. Control authors also expose properties that reference composite objects within a style or template, such that specific appearance aspects of the control can be adjusted by properties without having to adjust either the style or template. (You could style such properties, as the control consumer, but could also more easily set them directly as attributes in the XAML declarations.)

You can change the values of individual properties that have been set within a style. For example, you can set the Template property at run time even if this property has been set by a style. Or you can add setters to the collection in Setters. However, as soon as that style is placed in use by a loaded object, the Style should be considered sealed. You can detect this state by checking the value of IsSealed for the Style. A style is considered to be in use as soon as it is referenced by a loaded object that is connected to the object tree and the Silverlight root visual. This point in time can be detected when the object where the Style property is set raises its Loaded event. Attempting to change an individual property of an in-use style (such as a property in the Setters collection) throws an exception.

Classes derived from Control have an additional entry point that is useful to control authors in regard to style and template application. FrameworkElement defines the virtual callback OnApplyTemplate that is invoked prior to Loaded. Classes derived from Control can override this callback to adjust the property values that were set by templates before the style is in use, in order to apply non-templated values to the template-created properties for that instance of the control.

If queried at run time, the Style property does not return styles that come from an applied template. It only returns styles that were explicitly and previously set by the Style property. In addition, it does not return implicit styles. For more information about the implicit style concept, see Style.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

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

Platforms

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