Share via


Style 類別

定義

在某型別的各執行個體間啟用屬性、資源與事件處理常式的共用。

public ref class Style : System::Windows::Threading::DispatcherObject, System::Windows::Markup::IAddChild, System::Windows::Markup::INameScope
public ref class Style : System::Windows::Threading::DispatcherObject, System::Windows::Markup::IAddChild, System::Windows::Markup::INameScope, System::Windows::Markup::IQueryAmbient
[System.Windows.Localizability(System.Windows.LocalizationCategory.Ignore)]
[System.Windows.Markup.ContentProperty("Setters")]
[System.Windows.Markup.DictionaryKeyProperty("TargetType")]
public class Style : System.Windows.Threading.DispatcherObject, System.Windows.Markup.IAddChild, System.Windows.Markup.INameScope
[System.Windows.Localizability(System.Windows.LocalizationCategory.Ignore)]
[System.Windows.Markup.ContentProperty("Setters")]
[System.Windows.Markup.DictionaryKeyProperty("TargetType")]
public class Style : System.Windows.Threading.DispatcherObject, System.Windows.Markup.IAddChild, System.Windows.Markup.INameScope, System.Windows.Markup.IQueryAmbient
[<System.Windows.Localizability(System.Windows.LocalizationCategory.Ignore)>]
[<System.Windows.Markup.ContentProperty("Setters")>]
[<System.Windows.Markup.DictionaryKeyProperty("TargetType")>]
type Style = class
    inherit DispatcherObject
    interface INameScope
    interface IAddChild
[<System.Windows.Localizability(System.Windows.LocalizationCategory.Ignore)>]
[<System.Windows.Markup.ContentProperty("Setters")>]
[<System.Windows.Markup.DictionaryKeyProperty("TargetType")>]
type Style = class
    inherit DispatcherObject
    interface INameScope
    interface IAddChild
    interface IQueryAmbient
[<System.Windows.Localizability(System.Windows.LocalizationCategory.Ignore)>]
[<System.Windows.Markup.ContentProperty("Setters")>]
[<System.Windows.Markup.DictionaryKeyProperty("TargetType")>]
type Style = class
    inherit DispatcherObject
    interface IAddChild
    interface INameScope
    interface IQueryAmbient
Public Class Style
Inherits DispatcherObject
Implements IAddChild, INameScope
Public Class Style
Inherits DispatcherObject
Implements IAddChild, INameScope, IQueryAmbient
繼承
屬性
實作

範例

下列範例顯示會影響 Background 之 屬性的 Control樣式宣告。

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

若要套用上述樣式,請執行下列動作:

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

您也可以使用 TargetType 屬性,將樣式套用至指定型別的所有專案。 將目標類型新增至樣式表示您不再需要使用 語法來完整限定所設定 ClassName.PropertyName 的屬性。 下列範例會定義將套用至每個 TextBlock 項目的樣式。

<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" />
</Style>

注意

許多 WPF 控制項是由其他 WPF 控制件的組合所組成,因此建立套用至類型之所有控制件的樣式可能會造成廣泛的影響。 例如,如果您建立以 中的控件為目標TextBlock的樣式,則樣式會套用至畫布中的所有TextBlock控制件,即使 TextBlock 是另一個控件的一部分,例如 ListBoxCanvas

如需如何擴充或繼承自已定義樣式的資訊,請參閱 BasedOn 頁面。

備註

您可以在衍生自 FrameworkElementFrameworkContentElement的任何項目上設定 Style 。 樣式最常宣告為區段內的 Resources 資源。 因為樣式是資源,所以會遵守套用至所有資源的相同範圍規則,因此您宣告樣式會影響可套用的樣式。 例如,如果您在應用程式定義 XAML 檔案的根元素中宣告樣式,則可以在應用程式中的任何位置使用樣式。 如果您要建立導覽應用程式並在其中一個應用程式的 XAML 檔案中宣告樣式,則樣式只能用於該 XAML 檔案中。 如需資源範圍規則的詳細資訊,請參閱 XAML 資源

樣式宣告是由 Style 包含一或多個 Setter 物件的集合的物件所組成。 每個 Setter 都包含 PropertyValue。 屬性是要套用樣式之專案之屬性的名稱。 當樣式宣告為資源之後,就可以像任何其他資源一樣加以參考。

注意

如果 setter 集合中有多個 setter 具有相同 Property 屬性值,則會使用最後宣告的 setter。 同樣地,如果您在樣式中設定相同屬性的值,而且直接在元素上設定值,則直接在元素上設定的值會優先。

Windows Presentation Foundation (WPF) 樣式和範本化模型可讓您維護和共用外觀,以及呈現和邏輯的分隔。 樣式和範本化模型包含一套功能,可讓您自定義UI。 此套件功能包括 Style 類別,以及下列專案:

如需詳細資訊,請參閱 設定樣式和範本化

建構函式

Style()

初始化 Style 類別的新執行個體。

Style(Type)

初始化 Style 類別的新執行個體,以用於指定的 Type 上。

Style(Type, Style)

根據指定的 Style 初始化 Type 類別的新執行個體,以用於指定的 Style 上。

屬性

BasedOn

取得或設定做為目前樣式之基礎的已定義樣式。

Dispatcher

取得與這個 Dispatcher 關聯的 DispatcherObject

(繼承來源 DispatcherObject)
IsSealed

取得值,這個值表示樣式是否為唯讀而無法加以變更。

Resources

取得或設定可在這個樣式的範圍內使用的資源集合。

Setters

取得 SetterEventSetter 物件的集合。

TargetType

取得或設定適用此樣式的型別。

Triggers

取得 TriggerBase 物件的集合,該物件集合會根據指定的條件套用屬性值。

方法

CheckAccess()

判斷呼叫的執行是否可以存取這個 DispatcherObject

(繼承來源 DispatcherObject)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

傳回這個 Style 的雜湊程式碼。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
RegisterName(String, Object)

在目前的名稱範圍中註冊新的「名稱-物件」組。

Seal()

鎖定這個樣式以及所有 Factory 與觸發程序,使其無法變更。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
UnregisterName(String)

從名稱範圍中移除「名稱-物件」對應。

VerifyAccess()

請強制執行可以存取這個 DispatcherObject 的呼叫執行緒。

(繼承來源 DispatcherObject)

明確介面實作

IAddChild.AddChild(Object)

加入子物件。

IAddChild.AddText(String)

將節點的文字內容加入物件中。

INameScope.FindName(String)

傳回具有所提供之識別名稱的物件。

IQueryAmbient.IsAmbientPropertyAvailable(String)

查詢指定的環境屬性 (Ambient Property) 是否可用於目前的範圍。

適用於

另請參閱