FrameworkElement.Style Property

Gets or sets the style used by this element when it is rendered.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

member Style : Style with get, set
<object Style="{resourceExtension styleResourceKey}"/>

XAML Values

resourceExtension

One of the following: StaticResource, or DynamicResource. See XAML Resources.

styleResourceKey

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

NoteNote

Property element syntax is technically possible, but not recommended for most style scenarios. See Inline Styles and Templates. A binding reference using TemplateBinding or Binding is also possible, but uncommon.

Property Value

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

Identifier field

StyleProperty

Metadata properties set to true

AffectsMeasure

For controls, the current style is often provided by a default style from control theming, or from styles generally applied to controls of that type by resources at page or application level (an implicit style). This property does not set or return default (theme) styles, but it does return either an implicit style or an explicit style that is acting on the element. In the case of implicit or explicit styles, it does not matter whether the style is specified as a resource or defined locally.

Setting the styles has some restrictions. You can reset the entire Style property to a new Style at any time, which will force a layout recomposition. However, as soon as that style is placed in use by a loaded element, the Style should be considered sealed. Attempting to make a change to any individual property of an in-use style (such as anything within the collection of Setters) causes an exception to be thrown. A style that is defined in markup is considered to be in use as soon as it is loaded from a resource dictionary (for resources), or the page it is contained within is loaded (for inline styles).

Style is a dependency property with special precedence. The locally set style generally operates at the highest precedence in the property system. If the Style is null at this point, during loading the property system checks for implicit styles in local or application resources that specify that type. If the style is still null after this step, then the acting style for presentation purposes generally comes from the default (theme) style, but the default style is not returned in the Style property value. See Dependency Property Value Precedence or Styling and Templating.

The following example defines a style in a resource dictionary.

<Page.Resources>


...


<Style TargetType="Border" x:Key="PageBackground">
  <Setter Property="Background" Value="Blue"/>
</Style>


...


<Border Style="{StaticResource PageBackground}">


...


</Border>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft