次の方法で共有


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
継承
属性
実装

次の例は、ControlBackground プロパティに影響を与えるスタイル宣言を示しています。

<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 コントロールを対象とするスタイルを作成した場合、TextBlockListBoxなどの別のコントロールの一部である場合でも、そのスタイルはキャンバス内のすべてのTextBlock コントロールに適用されます。

定義されたスタイルを拡張または継承する方法については、 BasedOn ページを参照してください。

注釈

FrameworkElementまたはFrameworkContentElementから派生する任意の要素にStyleを設定できます。 スタイルは、最も一般的に、 Resources セクション内のリソースとして宣言されます。 スタイルはリソースであるため、すべてのリソースに適用されるのと同じスコープ規則に従うため、スタイルを宣言する場所は適用できる場所に影響します。 たとえば、アプリケーション定義 XAML ファイルのルート要素でスタイルを宣言した場合、スタイルはアプリケーション内の任意の場所で使用できます。 ナビゲーション アプリケーションを作成し、アプリケーションのいずれかの XAML ファイルでスタイルを宣言する場合、その XAML ファイルでのみスタイルを使用できます。 リソースのスコープ規則の詳細については、「 XAML リソース」を参照してください。

スタイル宣言は、1 つ以上のSetter オブジェクトのコレクションを含むStyle オブジェクトで構成されます。 各 Setter は、 PropertyValueで構成されます。 プロパティは、スタイルが適用される要素のプロパティの名前です。 スタイルをリソースとして宣言した後は、他のリソースと同様に参照できます。

同じ Property プロパティ値を持つセッター コレクションに複数のセッターがある場合は、最後に宣言されたセッターが使用されます。 同様に、スタイル内の同じプロパティの値を要素に直接設定すると、要素に設定された値が直接優先されます。

Windows Presentation Foundation (WPF) のスタイルおよびテンプレート モデルを使用すると、外観のメンテナンスと共有、およびプレゼンテーションとロジックの分離が可能になります。 スタイルおよびテンプレート モデルには、UI をカスタマイズできる一連の機能が含まれています。 この一連の機能には、 Style クラスと次のものが含まれます。

詳細については、「 スタイル設定とテンプレート」を参照してください。

コンストラクター

名前 説明
Style()

Style クラスの新しいインスタンスを初期化します。

Style(Type, Style)

指定したTypeで使用し、指定したStyleに基づいて、Style クラスの新しいインスタンスを初期化します。

Style(Type)

指定したTypeで使用するStyle クラスの新しいインスタンスを初期化します。

プロパティ

名前 説明
BasedOn

現在のスタイルの基礎となる定義済みのスタイルを取得または設定します。

Dispatcher

このDispatcherが関連付けられているDispatcherObjectを取得します。

(継承元 DispatcherObject)
IsSealed

スタイルが読み取り専用であり、変更できないかどうかを示す値を取得します。

Resources

このスタイルのスコープ内で使用できるリソースのコレクションを取得または設定します。

Setters

SetterオブジェクトとEventSetter オブジェクトのコレクションを取得します。

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)

指定したアンビエント プロパティが現在のスコープで使用できるかどうかをクエリします。

適用対象

こちらもご覧ください