共用方式為


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
繼承
屬性
實作

範例

以下範例展示了一個樣式宣告,該宣告會影響 BackgroundControl屬性。

<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 中的控制項 Canvas,該樣式會套用到畫布中的所有 TextBlock 控制項,即使該 TextBlock 控制項是另一個控制項的一部分,例如 ListBox

關於如何延伸或繼承定義風格的資訊,請參閱該 BasedOn 頁面。

備註

你可以對任何源自 FrameworkElementFrameworkContentElement的元素設定 a Style 。 風格最常被定義為該區塊中的 Resources 資源。 因為樣式是資源,它們遵循與所有資源相同的範圍規則,所以你宣告風格的位置會影響它能應用的位置。 例如,如果你在應用程式定義 XAML 檔案的根元素中宣告了樣式,該樣式可以在應用程式的任何地方使用。 如果你正在建立導覽應用程式,並在應用程式的 XAML 檔案中宣告該樣式,該樣式只能在該 XAML 檔案中使用。 欲了解更多資源範圍規則的資訊,請參見 XAML 資源

樣式宣告包含 Style 一個包含一個或多個 Setter 物件的集合。 每個由 Setter a Property 和 一個 Value組成。 屬性是樣式要套用的元素屬性名稱。 當樣式被宣告為資源後,就可以像其他資源一樣被引用。

備註

若集合中有多個具有相同 Property 屬性值的設定器,則使用最後宣告的設定器。 同樣地,如果你在風格和元素中直接設定同一屬性的值,則直接設定在該元素上的值會優先。

Windows Presentation Foundation(WPF)的樣式與範本模型允許維護與共享外觀,並分離呈現與邏輯。 樣式與模板模型包含一系列功能,讓你能自訂使用者介面。 這套功能包括該類別以及 Style 以下內容:

更多資訊請參見 樣式與模板

建構函式

名稱 Description
Style()

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

Style(Type, Style)

初始化一個新的類別實例 Style ,用於指定的 Type ,並基於指定的 Style

Style(Type)

初始化一個新的類別實例 Style ,用於指定的 Type

屬性

名稱 Description
BasedOn

取得或設定一個定義的風格,作為當前風格的基礎。

Dispatcher

了解 Dispatcher 這與此 DispatcherObject 有關。

(繼承來源 DispatcherObject)
IsSealed

會取得一個值,表示樣式是否為唯讀且無法更改。

Resources

取得或設定可在此風格範圍內使用的資源集合。

Setters

會得到一組 SetterEventSetter 物件。

TargetType

取得或設定此樣式所預期的類型。

Triggers

取得一組 TriggerBase 物件,根據指定條件套用屬性值。

方法

名稱 Description
CheckAccess()

判斷呼叫執行緒是否能存取此 DispatcherObject

(繼承來源 DispatcherObject)
Equals(Object)

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

(繼承來源 Object)
GetHashCode()

回傳此 Style的雜湊碼。

GetType()

取得目前實例的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

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

在目前的命名範圍中註冊一個新的名稱-物件對。

Seal()

會鎖定這種風格,所有工廠和觸發器都無法更改。

ToString()

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

(繼承來源 Object)
UnregisterName(String)

移除名稱範圍中的名稱-物件映射。

VerifyAccess()

強制呼叫執行緒能存取此 DispatcherObject

(繼承來源 DispatcherObject)

明確介面實作

名稱 Description
IAddChild.AddChild(Object)

新增一個子物件。

IAddChild.AddText(String)

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

INameScope.FindName(String)

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

IQueryAmbient.IsAmbientPropertyAvailable(String)

查詢目前範圍內是否有指定的環境屬性可用。

適用於

另請參閱