■ ControlTemplate 엘리먼트 : TargetType 속성 사용하기

----------------------------------------------------------------------------------------------------

<Window.Resources>

    <ControlTemplate

        x:Key="ButtonControlTemplateKey"

        TargetType="{x:Type Button}">

        <Border

            Name="border"

            BorderThickness="3"

            BorderBrush="Red"

            Background="{TemplateBinding Foreground}">

            <TextBlock

                Name="textBlock"

                Margin="{TemplateBinding Padding}"

                Foreground="{TemplateBinding Background}"

                FontStyle="Italic"

                Text="{TemplateBinding Content}" />

        </Border>

        <ControlTemplate.Triggers>

            <Trigger Property="IsMouseOver" Value="True">

                <Setter

                    TargetName="border"

                    Property="CornerRadius"

                    Value="12" />

                <Setter

                    TargetName="textBlock"

                    Property="FontWeight"

                    Value="Bold" />

            </Trigger>

            <Trigger Property="IsPressed" Value="True">

                <Setter

                    TargetName="border"

                    Property="Background"

                    Value="{Binding Path=Background}" />

                <Setter

                    TargetName="textBlock"

                    Property="Foreground"

                    Value="{Binding Path=Foreground}" />

            </Trigger>

        </ControlTemplate.Triggers>

    </ControlTemplate>

</Window.Resources>

<StackPanel>

    <Button

        Template="{StaticResource ButtonControlTemplateKey}"

        HorizontalAlignment="Center"

        Margin="24"

        Padding="10"

        FontSize="24">

        커스텀 템플리트 버튼

    </Button>

    <Button

        HorizontalAlignment="Center"

        Margin="24"

        Padding="10"

        FontSize="24">

        일반 버튼

    </Button>

    <Button

        Template="{StaticResource ButtonControlTemplateKey}"

        HorizontalAlignment="Center"

        Margin="24"

        Padding="10"

        FontSize="24">

        커스텀 템플리트 버튼

    </Button>

</StackPanel>

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요