次の方法で共有


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

次に、ButtonControlTemplate を設定する ButtonStyle を示します。

<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>

これが適用されると、ButtonEllipseとして表示されます。

Button ControlTemplate サンプル

上記の例のように、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>

この例では、ここに示されていないリソースを参照しています。 完全なサンプルについては、「ControlTemplates サンプルを使用したスタイル設定」を参照してください。 このサンプルでは、多くのコントロールのコントロール テンプレートの例を示します。これは、コントロール テンプレートの作成を開始するための最良の方法です。

注釈

ControlTemplate を使用すると、コントロールの視覚的な構造を指定できます。 コントロール作成者は既定の ControlTemplate を定義でき、アプリケーション作成者は ControlTemplate をオーバーライドしてコントロールの視覚的な構造を再構築できます。

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

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

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

XAML プロパティ要素の使用法

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

XAML 値

または派生クラスの ControlTemplate Object 要素を します。

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

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

コンストラクター

ControlTemplate()

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

ControlTemplate(Type)

指定したターゲット型を使用して、ControlTemplate クラスの新しいインスタンスを初期化します。

プロパティ

Dispatcher

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

(継承元 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)

適用対象

こちらもご覧ください