Style Class

Enables the sharing of properties, resources, and event handlers between instances of a type.

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

[ContentPropertyAttribute("Setters")]
[DictionaryKeyPropertyAttribute("TargetType")]
[LocalizabilityAttribute(LocalizationCategory.Ignore)]
public class Style : DispatcherObject, INameScope, 
	IAddChild, IQueryAmbient
<Style>
  Setters
</Style>

The Style type exposes the following members.

  NameDescription
Public methodStyle()Initializes a new instance of the Style class.
Public methodStyle(Type)Initializes a new instance of the Style class to use on the specified Type.
Public methodStyle(Type, Style)Initializes a new instance of the Style class to use on the specified Type and based on the specified Style.
Top

  NameDescription
Public propertyBasedOnGets or sets a defined style that is the basis of the current style.
Public propertyDispatcherGets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)
Public propertyIsSealedGets a value that indicates whether the style is read-only and cannot be changed.
Public propertyResourcesGets or sets the collection of resources that can be used within the scope of this style.
Public propertySettersGets a collection of Setter and EventSetter objects.
Public propertyTargetTypeGets or sets the type for which this style is intended.
Public propertyTriggersGets a collection of TriggerBase objects that apply property values based on specified conditions.
Top

  NameDescription
Public methodCheckAccessDetermines whether the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeReturns the hash code for this Style. (Overrides Object.GetHashCode().)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodRegisterNameRegisters a new name-object pair in the current namescope.
Public methodSealLocks this style and all factories and triggers so they cannot be changed.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodUnregisterNameRemoves a name-object mapping from the namescope.
Public methodVerifyAccessEnforces that the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Top

  NameDescription
Explicit interface implemetationPrivate methodIAddChild.AddChildInfrastructure. Adds a child object.
Explicit interface implemetationPrivate methodIAddChild.AddTextInfrastructure. Adds the text content of a node to the object.
Explicit interface implemetationPrivate methodINameScope.FindNameInfrastructure. Returns an object that has the provided identifying name.
Explicit interface implemetationPrivate methodIQueryAmbient.IsAmbientPropertyAvailableQueries whether a specified ambient property is available in the current scope.
Top

You can set a Style on any element that derives from FrameworkElement or FrameworkContentElement. A style is most commonly declared as a resource inside the Resources section. Because styles are resources, they obey the same scoping rules that apply to all resources, so where you declare a style affects where it can be applied. If, for instance, you declare the style in the root element of your application definition XAML file, the style can be used anywhere in your application. If you are creating a navigation application and declare the style in one of the application's XAML files, the style can be used only in that XAML file. For more information on scoping rules for resources, see XAML Resources.

The style declaration consists of a Style object that contains a collection of one or more Setter objects. Each Setter consists of a Property and a Value. The property is the name of the property of the element the style is to apply to. After the style is declared as a resource, it can then be referenced just like any other resource.

NoteNote

If there is more than one setter in the setter collection with the same Property property value, the setter that is declared last is used. Similarly, if you set a value for the same property in a style and on an element directly, the value set on the element directly takes precedence.

The Windows Presentation Foundation (WPF) styling and templating model allows maintenance and sharing of a look as well as the separation of presentation and logic. The styling and templating model includes a suite of features that enable you to customize your UI. This suite of features includes the Style class as well as the following:

For more information, see Styling and Templating.

The following example shows a style declaration that will affect the Background property of a Control.

<Style x:Key="Style1">
  <Setter Property="Control.Background" Value="Yellow"/>
</Style>

To apply the above style, do the following:

<Label Content="Yellow Background" Style="{StaticResource Style1}" />

You can also apply styles to all elements of a given type by using the TargetType property. Adding the target type to the style means that you no longer have to fully qualify the property you are setting with the ClassName.PropertyName syntax. The following example defines a style that will be applied to every TextBlock element.

<Style TargetType="{x:Type TextBlock}">
  <Setter Property="FontFamily" Value="Segoe Black" />
  <Setter Property="HorizontalAlignment" Value="Center" />
  <Setter Property="FontSize" Value="12pt" />
  <Setter Property="Foreground" Value="#777777" />
</Style>
NoteNote

Many WPF controls consist of a combination of other WPF controls, so creating a style that applies to all controls of a type can have broad impact. For instance, if you create a style that targets the TextBlock controls in a Canvas, the style is applied to all TextBlock controls in the canvas, even if the TextBlock is part of another control, such as a ListBox.

For information on how to extend or inherit from a defined style, see the BasedOn page.

.NET Framework

Supported in: 4.5.2, 4.5.1, 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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft