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 Attribute UsageXAML 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