.NET Framework Class Library
FrameworkContentElement..::.Style Property

Gets or sets the style to be used by this element. This is a dependency property.

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
Syntax

Visual Basic (Declaration)
Public Property Style As Style
Visual Basic (Usage)
Dim instance As FrameworkContentElement
Dim value As Style

value = instance.Style

instance.Style = value
C#
public Style Style { get; set; }
Visual C++
public:
property Style^ Style {
    Style^ get ();
    void set (Style^ value);
}
JScript
public function get Style () : Style
public function set Style (value : Style)
XAML Property Element Usage
<object>
  <object.Style>
    <Style .../>
  </object.Style>
</object>
XAML Attribute Usage
<object Style="{resourceExtension StyleResourceKey}"/>

XAML Values

resourceExtension

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

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. 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, nullNothingnullptra null reference (Nothing in Visual Basic). The default for a default-constructed FrameworkContentElement is nullNothingnullptra null reference (Nothing in Visual Basic).
Dependency Property Information

Identifier field

StyleProperty

Metadata properties set to true

AffectsMeasure

Remarks

The current style is often provided by a default style from theming, or from styles generally applied to objects 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 the implicit style or an explicit style. In the case of implicit or explicit styles, it does not matter whether the style is accessed 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 as defined resources that specify that type. If the style is still null after this step, then the style comes from the default (theme) style, but the default style is not returned in the Style property value. See Dependency Property Value Precedence.

Examples

The following example establishes a Resources collection on a FlowDocument root element and then references it as a resource as a specific style for a Paragraph.

XAML
<FlowDocument
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class=" SDKSample.Page2">
  <FlowDocument.Resources>
    <Style TargetType="{x:Type Paragraph}" x:Key="BluePara">
      <Setter Property="Background" Value="Blue"/>
      <Setter Property="FontSize" Value="18"/>
      <Setter Property="Foreground" Value="LightBlue"/>
      <Setter Property="FontFamily" Value="Trebuchet MS"/>
    </Style>
  </FlowDocument.Resources>
  <Paragraph Style="{StaticResource BluePara}">Lorem ipsum etc.</Paragraph>
</FlowDocument>
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0
See Also

Reference

Other Resources

Tags :


Page view tracker