ControlTemplate クラス

定義

コントロールの複数のインスタンスで共有できる、Control のビジュアル構造と動作特性を指定します。

public ref class ControlTemplate : System::Windows::FrameworkTemplate
[System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)]
[System.Windows.Markup.DictionaryKeyProperty("TargetType")]
public class ControlTemplate : System.Windows.FrameworkTemplate
[<System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)>]
[<System.Windows.Markup.DictionaryKeyProperty("TargetType")>]
type ControlTemplate = class
    inherit FrameworkTemplate
Public Class ControlTemplate
Inherits FrameworkTemplate
継承
属性

次に、次のButtonStyle値を設定する方法をControlTemplateButton示します。

<Style TargetType="Button">
  <!--Set to true to not get any properties from the themes.-->
  <Setter Property="OverridesDefaultStyle" Value="True"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <Grid>
          <Ellipse Fill="{TemplateBinding Background}"/>
          <ContentPresenter HorizontalAlignment="Center"
                            VerticalAlignment="Center"/>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

これが適用されると、次 Button のように Ellipse表示されます。

ボタン ControlTemplate のサンプル

上記の例のように a ControlTemplateプロパティを新しいControlTemplateプロパティに設定すると、テンプレート全体が置き換えられます。 フォーカスまたは押されたときの Button 外観は、置き換えるボタンの既定の外観の一部です。 したがって、必要に応じて、次の例のように、ボタンが押されたときにどのようなボタンが表示されるかを定義に入れることが必要になる場合があります。

<Style TargetType="Button">
  <Setter Property="SnapsToDevicePixels"
          Value="true" />
  <Setter Property="OverridesDefaultStyle"
          Value="true" />
  <Setter Property="FocusVisualStyle"
          Value="{StaticResource ButtonFocusVisual}" />
  <Setter Property="MinHeight"
          Value="23" />
  <Setter Property="MinWidth"
          Value="75" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <Border TextBlock.Foreground="{TemplateBinding Foreground}"
                x:Name="Border"
                CornerRadius="2"
                BorderThickness="1">
          <Border.BorderBrush>
            <LinearGradientBrush StartPoint="0,0"
                                 EndPoint="0,1">
              <LinearGradientBrush.GradientStops>
                <GradientStopCollection>
                  <GradientStop Color="{DynamicResource BorderLightColor}"
                                Offset="0.0" />
                  <GradientStop Color="{DynamicResource BorderDarkColor}"
                                Offset="1.0" />
                </GradientStopCollection>
              </LinearGradientBrush.GradientStops>
            </LinearGradientBrush>

          </Border.BorderBrush>
          <Border.Background>
            <LinearGradientBrush EndPoint="0.5,1"
                                 StartPoint="0.5,0">
              <GradientStop Color="{DynamicResource ControlLightColor}"
                            Offset="0" />
              <GradientStop Color="{DynamicResource ControlMediumColor}"
                            Offset="1" />
            </LinearGradientBrush>
          </Border.Background>
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
              <VisualStateGroup.Transitions>
                <VisualTransition GeneratedDuration="0:0:0.5" />
                <VisualTransition GeneratedDuration="0"
                                  To="Pressed" />
              </VisualStateGroup.Transitions>
              <VisualState x:Name="Normal" />
              <VisualState x:Name="MouseOver">
                <Storyboard>
                  <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).
                      (GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                                Storyboard.TargetName="Border">
                    <EasingColorKeyFrame KeyTime="0"
                                         Value="{StaticResource ControlMouseOverColor}" />
                  </ColorAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Pressed">
                <Storyboard>
                  <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).
                      (GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                                Storyboard.TargetName="Border">
                    <EasingColorKeyFrame KeyTime="0"
                                         Value="{StaticResource ControlPressedColor}" />
                  </ColorAnimationUsingKeyFrames>
                  <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).
                      (GradientBrush.GradientStops)[0].(GradientStop.Color)"
                                                Storyboard.TargetName="Border">
                    <EasingColorKeyFrame KeyTime="0"
                                         Value="{StaticResource PressedBorderDarkColor}" />
                  </ColorAnimationUsingKeyFrames>
                  <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).
                      (GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                                Storyboard.TargetName="Border">
                    <EasingColorKeyFrame KeyTime="0"
                                         Value="{StaticResource PressedBorderLightColor}" />
                  </ColorAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Disabled">
                <Storyboard>
                  <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).
                      (GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                                Storyboard.TargetName="Border">
                    <EasingColorKeyFrame KeyTime="0"
                                         Value="{StaticResource DisabledControlDarkColor}" />
                  </ColorAnimationUsingKeyFrames>
                  <ColorAnimationUsingKeyFrames
                      Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                                                Storyboard.TargetName="Border">
                    <EasingColorKeyFrame KeyTime="0"
                                         Value="{StaticResource DisabledForegroundColor}" />
                  </ColorAnimationUsingKeyFrames>
                  <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).
                      (GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                                Storyboard.TargetName="Border">
                    <EasingColorKeyFrame KeyTime="0"
                                         Value="{StaticResource DisabledBorderDarkColor}" />
                  </ColorAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <ContentPresenter Margin="2"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            RecognizesAccessKey="True" />
        </Border>
        <ControlTemplate.Triggers>
          <Trigger Property="IsDefault"
                   Value="true">

            <Setter TargetName="Border"
                    Property="BorderBrush">
              <Setter.Value>
                <LinearGradientBrush StartPoint="0,0"
                                     EndPoint="0,1">
                  <GradientBrush.GradientStops>
                    <GradientStopCollection>
                      <GradientStop Color="{DynamicResource DefaultBorderBrushLightBrush}"
                                    Offset="0.0" />
                      <GradientStop Color="{DynamicResource DefaultBorderBrushDarkColor}"
                                    Offset="1.0" />
                    </GradientStopCollection>
                  </GradientBrush.GradientStops>
                </LinearGradientBrush>

              </Setter.Value>
            </Setter>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

この例では、ここに示されていないリソースを参照しています。 完全なサンプルについては、Styling with ControlTemplates Sampleを参照してください。 このサンプルでは、多くのコントロールのコントロール テンプレートの例を示し、コントロール テンプレートの作成を開始するための最適な方法です。

注釈

ControlTemplateこれにより、コントロールの視覚的な構造を指定できます。 コントロールの作成者は既定値 ControlTemplate を定義でき、アプリケーション作成者はコントロールの ControlTemplate ビジュアル構造を再構築するためにオーバーライドできます。

コントロール テンプレートは、WPF スタイルとテンプレート モデルによって提供される多くの機能の 1 つです。 スタイルとテンプレート モデルは、多くの場合、独自のコントロールを記述する必要がない、非常に優れた柔軟性を提供します。 アプリケーション作成者が、コントロールの視覚化を変更する場合、または既存のコントロールを置き換える ControlTemplate 場合は、「 スタイルとテンプレート 」トピックの例と詳細な説明を参照してください。

独自のコントロールを作成する場合は、「コントロールの作成の概要」の「カスタム コントロールを作成 する」を参照してください。

A ControlTemplate は、スタイルを含む外部のユーザーやオブジェクトには見えない自己完結型の実装の詳細単位を意図しています。 コントロール テンプレートのコンテンツを操作する唯一の方法は、同じコントロール テンプレート内からです。

XAML プロパティ要素の使用

<ControlTemplate>  
  <VisualTreeRootNode>
    VisualTreeNodeContents  
  </VisualTreeRootNode>  
</ControlTemplate>  

XAML 値

ControlTemplate
派生クラスの ControlTemplate オブジェクト要素。

VisualTreeRootNode
(または派生クラス) の直接の ControlTemplate 子としての 1 つの XAML 要素。 テンプレートには 1 つのルート ノードが必要です。 有用なテンプレートを生成するために、 VisualTreeRootNode として選択された要素は、独自のコンテンツ モデル (多くの場合、複数の子要素をサポートするモデル) をサポートすることが期待されます。

VisualTreeNodeContents
目的のテンプレートを完成する 1 つ以上の要素。 VisualTreeRootNode として選択された要素が 1 つの子のみをサポートする場合、VisualTreeNodeContents として宣言できる要素は 1 つだけです。 選択した VisualTreeRootNode でテキスト コンテンツ プロパティがサポートされている場合は、テキスト コンテンツを提供することも可能です (一般的ではありません)。

コンストラクター

ControlTemplate()

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

ControlTemplate(Type)

指定された対象の型を使用して ControlTemplate クラスの新しいインスタンスを初期化します。

プロパティ

Dispatcher

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

(継承元 DispatcherObject)
HasContent

最適化されたコンテンツがこのテンプレートに含まれているかどうかを示す値を取得します。

(継承元 FrameworkTemplate)
IsSealed

このオブジェクトが不変の状態で変更不可能であるかどうかを示す値を取得します。

(継承元 FrameworkTemplate)
Resources

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

(継承元 FrameworkTemplate)
TargetType

この ControlTemplate が対象としている型を取得または設定します。

Template

ライターがテンプレートを定義または適用するときに、テンプレートの XAML ノードを記録または再生するオブジェクトへの参照を取得または設定します。

(継承元 FrameworkTemplate)
Triggers

プロパティの変更を適用する、または指定した条件に基づいてアクションを実行する TriggerBase オブジェクトのコレクションを取得します。

VisualTree

テンプレートのルート ノードを取得または設定します。

(継承元 FrameworkTemplate)

メソッド

CheckAccess()

呼び出し元のスレッドがこの DispatcherObject にアクセスできるかどうかを確認します。

(継承元 DispatcherObject)
Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
FindName(String, FrameworkElement)

このテンプレート内で定義された、指定した名前に関連付けられている要素を検索します。

(継承元 FrameworkTemplate)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
LoadContent()

テンプレートのコンテンツをオブジェクトのインスタンスとして読み込み、コンテンツのルート要素を返します。

(継承元 FrameworkTemplate)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
RegisterName(String, Object)

現在の名前スコープに新しい名前とオブジェクトのペアを登録します。

(継承元 FrameworkTemplate)
Seal()

テンプレートをロックし、変更できないようにします。

(継承元 FrameworkTemplate)
ShouldSerializeResources(XamlDesignerSerializationManager)

シリアル化プロセスが、このクラスのインスタンスの Resources プロパティの値をシリアル化する必要があるかどうかを示す値を返します。

(継承元 FrameworkTemplate)
ShouldSerializeVisualTree()

シリアル化プロセスが、このクラスのインスタンスの VisualTree プロパティの値をシリアル化する必要があるかどうかを示す値を返します。

(継承元 FrameworkTemplate)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)
UnregisterName(String)

XAML 名前スコープから名前とオブジェクトのマッピングを削除します。

(継承元 FrameworkTemplate)
ValidateTemplatedParent(FrameworkElement)

規則のセットに対してテンプレートが適用される親をチェックします。

VerifyAccess()

呼び出し元のスレッドがこの DispatcherObject にアクセスできるように強制します。

(継承元 DispatcherObject)

明示的なインターフェイスの実装

INameScope.FindName(String)

指定された識別名を持つオブジェクトを返します。

(継承元 FrameworkTemplate)
IQueryAmbient.IsAmbientPropertyAvailable(String)

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

(継承元 FrameworkTemplate)

適用対象

こちらもご覧ください