Style 类

定义

在类型的不同实例之间共享属性、资源和事件处理程序。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
[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
type Style = class
    inherit DispatcherObject
    interface INameScope
    interface IAddChild
    interface IQueryAmbient
Public Class Style
Inherits DispatcherObject
Implements IAddChild, INameScope, IQueryAmbient
继承
属性
实现

示例

下面的示例演示将影响 ControlBackground 属性的样式声明。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"/>
</Style>

若要应用以上样式,请执行以下操作: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" />
</Style>

备注

许多 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. 例如,如果您创建一个样式,该样式以 Canvas中的 TextBlock 控件为目标,则该样式将应用于画布中的所有 TextBlock 控件,即使 TextBlock 是另一个控件(如 ListBox)的一部分。For 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.

注解

您可以对从 FrameworkElementFrameworkContentElement派生的任何元素设置 StyleYou 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 都包含 PropertyValueEach 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 集合中有多个具有相同 Property 属性值的 setter,则使用最后声明的 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 类的新实例。Initializes a new instance of the Style class.

Style(Type)

初始化 Style 类的一个新实例,以便在指定的 Type 上使用。Initializes a new instance of the Style class to use on the specified Type.

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

获取或设置一个作为当前样式的基准的已定义样式。Gets or sets a defined style that is the basis of the current style.

Dispatcher

获取与此 Dispatcher 关联的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(继承自 DispatcherObject)
IsSealed

获取一个值,该值指示样式是否为只读(无法更改)。Gets a value that indicates whether the style is read-only and cannot be changed.

Resources

获取或设置可在此样式的范围内使用的资源的集合。Gets or sets the collection of resources that can be used within the scope of this style.

Setters

获取 SetterEventSetter 对象的集合。Gets a collection of Setter and EventSetter objects.

TargetType

获取或设置此样式所针对的类型。Gets or sets the type for which this style is intended.

Triggers

获取基于指定条件应用属性值的 TriggerBase 对象的集合。Gets a collection of TriggerBase objects that apply property values based on specified conditions.

方法

CheckAccess()

确定调用线程是否可以访问此 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(继承自 DispatcherObject)
Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

返回此 Style 的哈希代码。Returns the hash code for this Style.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
RegisterName(String, Object)

在当前名称领域内注册一个新的名称-对象对。Registers a new name-object pair in the current namescope.

Seal()

锁定此样式和所有工厂与触发器,使得它们无法被更改。Locks this style and all factories and triggers so they cannot be changed.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
UnregisterName(String)

从名称领域内移除名称-对象映射。Removes a name-object mapping from the namescope.

VerifyAccess()

强制调用线程具有此 DispatcherObject 的访问权限。Enforces that the calling thread has access to this DispatcherObject.

(继承自 DispatcherObject)

显式接口实现

IAddChild.AddChild(Object)

添加子对象。Adds a child object.

IAddChild.AddText(String)

将节点的文本内容添加到对象。Adds the text content of a node to the object.

INameScope.FindName(String)

返回具有提供的标识名称的对象。Returns an object that has the provided identifying name.

IQueryAmbient.IsAmbientPropertyAvailable(String)

查询当前范围中是否有指定的环境属性。Queries whether a specified ambient property is available in the current scope.

ISealable.CanSeal
ISealable.IsSealed
ISealable.Seal()

适用于

另请参阅