다음을 통해 공유


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 스타일 지정 및 템플릿 모델에서 제공하는 많은 기능 중 하나입니다. 스타일 지정 및 템플릿 모델은 많은 경우 고유한 컨트롤을 작성할 필요가 없는 뛰어난 유연성을 제공합니다. 컨트롤의 시각화를 변경하거나 기존 컨트롤의 ControlTemplate 바꾸려는 애플리케이션 작성자는 스타일 지정 및 템플릿 항목에서 예제 및 심층 토론을 참조하세요.

고유한 컨트롤을 작성하는 경우 컨트롤 작성 개요"사용자 지정 컨트롤 만들기"를 참조하세요.

ControlTemplate 스타일을 포함하여 외부 사용자 및 개체에 보이지 않는 자체 포함 구현 세부 정보 단위로 사용됩니다. 컨트롤 템플릿의 콘텐츠를 조작하는 유일한 방법은 동일한 컨트롤 템플릿 내에서입니다.

XAML 속성 요소 사용

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

XAML 값

ControlTemplate 또는 파생 클래스에 대한 ControlTemplate Object 요소입니다.

visualTreeRootNode를 단일 XAML 요소를 ControlTemplate 직계 자식(또는 파생 클래스)으로. 템플릿에는 단일 루트 노드가 있어야 합니다. 유용한 템플릿을 생성하기 위해 VisualTreeRootNode 선택한 요소는 여러 자식 요소를 지원하는 모델인 자체 콘텐츠 모델을 지원해야 합니다.

VisualTreeNodeContents는 원하는 템플릿을 완료하는 하나 이상의 요소를. VisualTreeRootNode로 선택한 요소가 단일 자식만 지원하는 경우VisualTreeNodeContents로 선언된 요소는 하나만 있을 수 있습니다. 선택한 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)

serialization 프로세스가 이 클래스의 인스턴스에서 Resources 속성 값을 serialize해야 하는지 여부를 나타내는 값을 반환합니다.

(다음에서 상속됨 FrameworkTemplate)
ShouldSerializeVisualTree()

serialization 프로세스가 이 클래스의 인스턴스에서 VisualTree 속성 값을 serialize해야 하는지 여부를 나타내는 값을 반환합니다.

(다음에서 상속됨 FrameworkTemplate)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)
UnregisterName(String)

XAML 이름 범위에서 이름/개체 매핑을 제거합니다.

(다음에서 상속됨 FrameworkTemplate)
ValidateTemplatedParent(FrameworkElement)

템플릿이 지정된 부모를 규칙 집합에 대해 확인합니다.

VerifyAccess()

호출 스레드가 이 DispatcherObject액세스할 수 있도록 합니다.

(다음에서 상속됨 DispatcherObject)

명시적 인터페이스 구현

INameScope.FindName(String)

제공된 식별 이름이 있는 개체를 반환합니다.

(다음에서 상속됨 FrameworkTemplate)
IQueryAmbient.IsAmbientPropertyAvailable(String)

지정된 앰비언트 속성을 현재 범위에서 사용할 수 있는지 여부를 쿼리합니다.

(다음에서 상속됨 FrameworkTemplate)

적용 대상

추가 정보