FrameworkElement.Style FrameworkElement.Style FrameworkElement.Style FrameworkElement.Style Property

定義

取得或設定這個項目呈現時要使用的樣式。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 Attribute UsageXAML 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. 索引鍵參考中的現有資源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. 使用繫結參考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

適用於