FrameworkElement.Style 属性

定义

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

public:
 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.

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

注解

对于控件,当前样式通常由控件主题中的默认样式提供,或者由通常按页或应用程序级别的资源应用于该类型控件的样式(隐式样式)提供。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,则用于演示的用途样式通常来自默认(主题)样式,但在 Style 属性值中不返回默认样式。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.

XAML 属性用法XAML Attribute Usage

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

XAML 属性元素用法XAML Property Element Usage

XAML 值XAML Values

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

styleResourceKeystyleResourceKey
标识所请求的样式的键。The key that identifies the style being requested. 该键引用 ResourceDictionary中的现有资源。The 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. 也可以使用TemplateBindingBinding 的绑定引用,但情况并不常见。A binding reference using TemplateBinding or Binding is also possible, but uncommon.

依赖项属性信息Dependency Property Information

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

适用于