FrameworkElement.Style 属性


获取或设置此元素呈现时所使用的样式。Gets or sets the style used by this element when it is rendered.

 property System::Windows::Style ^ Style { System::Windows::Style ^ get(); void set(System::Windows::Style ^ value); };
public System.Windows.Style Style { get; set; }
member this.Style : System.Windows.Style with get, set
Public Property Style As Style



若存在,则为此元素适用的非默认样式。The applied, nondefault style for the element, if present. 否则为 nullOtherwise, null. 对于默认构造的 FrameworkElement,其默认值为 nullThe default for a default-constructed FrameworkElement is null.


下面的示例定义资源字典中的样式。The following example defines a style in a resource dictionary.

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


对于控件,当前样式通常由控件主题中的默认样式提供,或者由通常由页面或应用程序级别的资源应用于该类型的控件的样式提供 (隐式样式) 。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. 可随时将整个属性重置 Style 为新的 Style ,这将强制进行布局重新组合。You can reset the entire Style property to a new Style at any time, which will force a layout recomposition. 但是,一旦加载的元素使用该样式,就 Style 应将其视为密封。However, as soon as that style is placed in use by a loaded element, the Style should be considered sealed. 尝试对使用中的样式的任何单个属性进行更改 (如) 集合内的任何内容 Setters 会导致引发异常。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 是具有特殊优先级的依赖项属性。Style is a dependency property with special precedence. 本地设置的样式通常在属性系统中具有最高的优先级。The locally set style generally operates at the highest precedence in the property system. 如果 Style 此时为 null,则在加载过程中,属性系统会检查本地或指定该类型的应用程序资源中的隐式样式。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. 如果在执行此步骤后样式仍然为 null,则用于演示的操作样式通常来自默认 (主题) 样式,但在属性值中不返回默认样式 StyleIf 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.

XAML 属性用法XAML Attribute Usage

<object Style="{resourceExtension styleResourceKey}"/>  

XAML 属性元素用法XAML Property Element Usage


以下项之一:、或。One of the following: , or . 请参阅 XAML 资源See XAML Resources.

标识所请求的样式的键。The key that identifies the style being requested. 键引用中的现有资源 ResourceDictionaryThe key refers to an existing resource in a ResourceDictionary.


属性元素语法在技术上是可行的,但不建议用于大多数样式方案。Property element syntax is technically possible, but not recommended for most style scenarios. 请参阅 内联样式和模板See Inline Styles and Templates. 使用 TemplateBinding 或的绑定引用 Binding 也可能发生,但情况并不常见。A binding reference using TemplateBinding or Binding is also possible, but uncommon.

依赖项属性信息Dependency Property Information

标识符字段Identifier field StyleProperty
元数据属性设置为 trueMetadata properties set to true AffectsMeasure