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 控件的组合组成,因此创建一个适用于某一类型的所有控件的样式可能会产生广泛的影响。 例如,如果创建一个以 中的Canvas控件为目标的TextBlock样式,该样式将应用于画布中的所有TextBlock控件,即使 TextBlock 是另一个控件的一ListBox部分,例如 。

有关如何扩展或继承已定义的样式的信息,请参阅页面 BasedOn

注解

可以在派生自 FrameworkElementFrameworkContentElement的任何元素上设置 Style 。 样式最常声明为 节中的 Resources 资源。 由于样式是资源,因此它们遵循适用于所有资源的相同范围规则,因此声明样式的位置会影响应用样式的位置。 例如,如果在应用程序定义 XAML 文件的根元素中声明样式,则可以在应用程序中的任意位置使用该样式。 如果要创建导航应用程序并在应用程序的 XAML 文件中声明样式,则只能在该 XAML 文件中使用该样式。 有关资源范围规则的详细信息,请参阅 XAML 资源

样式声明包含一个 Style 对象,该对象包含一个或多个 Setter 对象的集合。 每个 都SetterProperty由 和 Value组成。 属性是样式要应用到的元素的属性的名称。 将样式声明为资源后,可以像引用任何其他资源一样对其进行引用。

注意

如果 setter 集合 Property 中有多个具有相同属性值的 setter,则使用最后声明的 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()

锁定此样式和所有工厂与触发器,使得它们无法被更改。

ToString()

返回表示当前对象的字符串。

(继承自 Object)
UnregisterName(String)

从名称领域内移除名称-对象映射。

VerifyAccess()

强制调用线程具有此 DispatcherObject 的访问权限。

(继承自 DispatcherObject)

显式接口实现

IAddChild.AddChild(Object)

添加子对象。

IAddChild.AddText(String)

将节点的文本内容添加到对象。

INameScope.FindName(String)

返回具有提供的标识名称的对象。

IQueryAmbient.IsAmbientPropertyAvailable(String)

查询当前范围中是否有指定的环境属性。

适用于

另请参阅