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
[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
继承
属性
实现

示例

下面的示例演示将影响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"/>
</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. 例如, 如果您创建一个样式, 该样式面向TextBlock Canvas中的控件, 则该样式将应用于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.

注解

可以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 集合中有多个具有相同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. 样式设置和模板化模型包含一套功能, 使你能够自定义UIUI你的。The 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 的浅表副本。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()

锁定此样式和所有工厂与触发器,使得它们无法被更改。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)

查询当前范围中是否有指定的环境属性。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()

适用于

另请参阅