Style Style Style Style Class


在某型別的各執行個體間啟用屬性、資源與事件處理常式的共用。Enables the sharing of properties, resources, and event handlers between instances of a type.

public ref class Style : System::Windows::Threading::DispatcherObject, System::Windows::Markup::IAddChild, System::Windows::Markup::INameScope, System::Windows::Markup::IQueryAmbient
public class Style : System.Windows.Threading.DispatcherObject, System.Windows.Markup.IAddChild, System.Windows.Markup.INameScope, System.Windows.Markup.IQueryAmbient
type Style = class
    inherit DispatcherObject
    interface INameScope
    interface IAddChild
    interface IQueryAmbient
Public Class Style
Inherits DispatcherObject
Implements IAddChild, INameScope, IQueryAmbient


下列範例顯示會影響Background Control之屬性的樣式宣告。The following example shows a style declaration that will affect the Background property of a Control.

<Style x:Key="Style1">
  <Setter Property="Control.Background" Value="Yellow"/>

若要套用上述樣式, 請執行下列動作:To apply the above style, do the following:

<Label Content="Yellow Background" Style="{StaticResource Style1}" />

您也可以使用TargetType屬性, 將樣式套用至指定類型的所有元素。You can also apply styles to all elements of a given type by using the TargetType property. 將目標型別新增至樣式, 表示您不再需要使用ClassName.PropertyName語法來完整限定您所設定的屬性。Adding the target type to the style means that you no longer have to fully qualify the property you are setting with the ClassName.PropertyName syntax. 下列範例會定義將套用至每個TextBlock元素的樣式。The following example defines a style that will be applied to every TextBlock element.

<Style TargetType="{x:Type TextBlock}">
  <Setter Property="FontFamily" Value="Segoe Black" />
  <Setter Property="HorizontalAlignment" Value="Center" />
  <Setter Property="FontSize" Value="12pt" />
  <Setter Property="Foreground" Value="#777777" />


許多WPFWPF控制項都是由其他WPFWPF控制項的組合所組成, 因此, 建立適用于類型之所有控制項的樣式可能會有廣泛的影響。Many WPFWPF controls consist of a combination of other WPFWPF controls, so creating a style that applies to all controls of a type can have broad impact. 例如, 如果您TextBlock建立以Canvas中的控制項為目標的樣式, 則樣式會套用至畫布中TextBlock的所有TextBlock控制項, 即使是另一個控制項的一部分, 例如ListBoxFor instance, if you create a style that targets the TextBlock controls in a Canvas, the style is applied to all TextBlock controls in the canvas, even if the TextBlock is part of another control, such as a ListBox.

如需如何從定義的樣式延伸或繼承的詳細資訊, 請BasedOn參閱頁面。For information on how to extend or inherit from a defined style, see the BasedOn page.


您可以Style在衍生自FrameworkElementFrameworkContentElement的任何專案上設定。You can set a Style on any element that derives from FrameworkElement or FrameworkContentElement. 樣式最常宣告為Resources區段內的資源。A style is most commonly declared as a resource inside the Resources section. 因為樣式是資源, 所以它們會遵守適用于所有資源的相同範圍規則, 因此您宣告樣式會影響可以套用的位置。Because styles are resources, they obey the same scoping rules that apply to all resources, so where you declare a style affects where it can be applied. 例如, 如果您在應用程式定義XAMLXAML檔的根項目中宣告樣式, 則可以在應用程式中的任何位置使用樣式。If, for instance, you declare the style in the root element of your application definition XAMLXAML file, the style can be used anywhere in your application. 如果您要建立導覽應用程式, 並在其中一個應用程式的XAMLXAML檔案中宣告樣式, 則樣式只能用在該XAMLXAML檔案中。If you are creating a navigation application and declare the style in one of the application's XAMLXAML files, the style can be used only in that XAMLXAML file. 如需資源範圍規則的詳細資訊, 請參閱XAML 資源For more information on scoping rules for resources, see XAML Resources.

樣式宣告是由包含一Style或多個Setter物件之集合的物件所組成。The style declaration consists of a Style object that contains a collection of one or more Setter objects. Setter個都是Property由和Value組成。Each Setter consists of a Property and a Value. 屬性是要套用樣式之元素的屬性名稱。The property is the name of the property of the element the style is to apply to. 在樣式宣告為資源之後, 就可以像任何其他資源一樣地加以參考。After the style is declared as a resource, it can then be referenced just like any other resource.


如果 setter 集合中有一個以上的 setter 具有相同Property的屬性值, 則會使用最後宣告的 setter。If there is more than one setter in the setter collection with the same Property property value, the setter that is declared last is used. 同樣地, 如果您在樣式中設定相同屬性的值, 並直接在專案上設定, 則在元素上設定的值會直接優先。Similarly, if you set a value for the same property in a style and on an element directly, the value set on the element directly takes precedence.

Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF)樣式設定和範本化模型可讓您維護和共用外觀, 以及分隔簡報和邏輯。The Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) styling and templating model allows maintenance and sharing of a look as well as the separation of presentation and logic. 樣式設定和範本化模型包含一套功能, 可讓您自訂UIUIThe styling and templating model includes a suite of features that enable you to customize your UIUI. 此功能套件包含Style類別以及下列各項:This suite of features includes the Style class as well as the following:

如需詳細資訊,請參閱 設定樣式和範本For more information, see Styling and Templating.


Style() Style() Style() Style()

初始化 Style 類別的新執行個體。Initializes a new instance of the Style class.

Style(Type) Style(Type) Style(Type) Style(Type)

初始化 Style 類別的新執行個體,以用於指定的 Type 上。Initializes a new instance of the Style class to use on the specified Type.

Style(Type, Style) Style(Type, Style) Style(Type, Style) Style(Type, Style)

根據指定的 Style 初始化 Type 類別的新執行個體,以用於指定的 Style 上。Initializes a new instance of the Style class to use on the specified Type and based on the specified Style.


BasedOn BasedOn BasedOn BasedOn

取得或設定做為目前樣式之基礎的已定義樣式。Gets or sets a defined style that is the basis of the current style.

Dispatcher Dispatcher Dispatcher Dispatcher

取得與這個 Dispatcher 關聯的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(Inherited from DispatcherObject)
IsSealed IsSealed IsSealed IsSealed

取得值,這個值表示樣式是否為唯讀而無法加以變更。Gets a value that indicates whether the style is read-only and cannot be changed.

Resources Resources Resources Resources

取得或設定可在這個樣式的範圍內使用的資源集合。Gets or sets the collection of resources that can be used within the scope of this style.

Setters Setters Setters Setters

取得 SetterEventSetter 物件的集合。Gets a collection of Setter and EventSetter objects.

TargetType TargetType TargetType TargetType

取得或設定適用此樣式的型別。Gets or sets the type for which this style is intended.

Triggers Triggers Triggers Triggers

取得 TriggerBase 物件的集合,該物件集合會根據指定的條件套用屬性值。Gets a collection of TriggerBase objects that apply property values based on specified conditions.


CheckAccess() CheckAccess() CheckAccess() CheckAccess()

判斷呼叫的執行是否可以存取這個 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

傳回這個 Style 的雜湊程式碼。Returns the hash code for this Style.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
RegisterName(String, Object) RegisterName(String, Object) RegisterName(String, Object) RegisterName(String, Object)

在目前的名稱範圍中註冊新的「名稱-物件」組。Registers a new name-object pair in the current namescope.

Seal() Seal() Seal() Seal()

鎖定這個樣式以及所有 Factory 與觸發程序,使其無法變更。Locks this style and all factories and triggers so they cannot be changed.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)
UnregisterName(String) UnregisterName(String) UnregisterName(String) UnregisterName(String)

從名稱範圍中移除「名稱-物件」對應。Removes a name-object mapping from the namescope.

VerifyAccess() VerifyAccess() VerifyAccess() VerifyAccess()

請強制執行可以存取這個 DispatcherObject 的呼叫執行緒。Enforces that the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)


IAddChild.AddChild(Object) IAddChild.AddChild(Object) IAddChild.AddChild(Object) IAddChild.AddChild(Object)

加入子物件。Adds a child object.

IAddChild.AddText(String) IAddChild.AddText(String) IAddChild.AddText(String) IAddChild.AddText(String)

將節點的文字內容加入物件中。Adds the text content of a node to the object.

INameScope.FindName(String) INameScope.FindName(String) INameScope.FindName(String) INameScope.FindName(String)

傳回具有所提供之識別名稱的物件。Returns an object that has the provided identifying name.

IQueryAmbient.IsAmbientPropertyAvailable(String) IQueryAmbient.IsAmbientPropertyAvailable(String) IQueryAmbient.IsAmbientPropertyAvailable(String) IQueryAmbient.IsAmbientPropertyAvailable(String)

查詢指定的環境屬性 (Ambient Property) 是否可用於目前的範圍。Queries whether a specified ambient property is available in the current scope.

ISealable.CanSeal ISealable.CanSeal ISealable.CanSeal ISealable.CanSeal
ISealable.IsSealed ISealable.IsSealed ISealable.IsSealed ISealable.IsSealed
ISealable.Seal() ISealable.Seal() ISealable.Seal() ISealable.Seal()