Aracılığıyla paylaş


FrameworkElement.Style Property

Definition

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

public:
 property Style ^ Style { Style ^ get(); void set(Style ^ value); };
Style Style();

void Style(Style value);
public Style Style { get; set; }
var style = frameworkElement.style;
frameworkElement.style = style;
Public Property Style As Style
<frameworkElement Style="{StaticResource styleResourceKey}"/>

Property Value

The applied style for the object, if present; otherwise, null. The default for a default-constructed FrameworkElement is null.

Remarks

Setting the Style property directly is not the most common way to style a UI element's appearance. Instead, you influence a UI element's appearance through one of the these techniques:

  • You often set individual UI properties of a UI element as attributes in XAML. FrameworkElement.Style only applies to the current element, and any value from a StyleFrameworkElement.Style is overwritten by a local property value, so setting the local value is more direct and more predictable. For more info on how a Style value and a local property value relate, see Dependency properties overview.
  • Control authors typically write a control that is templatable, but also comes with a default template. Your app can use the control and change its appearance by providing a new ControlTemplate, which is part of a Style defined in resources. This style is typically applied using the implicit style feature, not by setting Style. For more info, see Styling controls.
  • Control authors also expose properties that reference parts of a control that can have styles or values applied to just that part, so you don't have to totally retemplate the control. Here you're setting a more specific property, not FrameworkElement.Style.
  • The visual state model frequently resets the style of control at run time in response to changes in states that manifest themselves to the users visually. For example, control templates typically have a "Focused" state that adds a visual focus indicator to the control appearance so that a keyboard user can see which element in UI has the current keyboard focus. For more info on the visual state concept, see Quickstart: Control templates.

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 of a displayed page of UI. A Style and its Setters can also be considered sealed when the object using that style 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.

If queried at run time, the Style property does not return styles that come from an applied template, or active visual states. It only returns styles that were explicitly set by the Style property. In addition, it does not return implicit styles.

Applies to