첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ ControlTemplate 엘리먼트 : RelativeSource 태그 확장에서 TemplatedParent 사용하기

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

<Window

    x:Class="WpfApplication1.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Window.Resources>

        <Style TargetType="{x:Type Button}">

            <Setter Property="Template">

                <Setter.Value>

                    <ControlTemplate TargetType="{x:Type Button}">

                        <Border

                            x:Name="border"

                            BorderThickness="2"

                            BorderBrush="Black"

                            CornerRadius="20">

                            <Border.Background>

                                <LinearGradientBrush

                                    StartPoint="0 0.5"

                                    EndPoint="1 0.5">

                                    <GradientStop Offset="0.0" Color="{Binding Background.Color, RelativeSource={RelativeSource TemplatedParent}}" />

                                    <GradientStop Offset="0.9" Color="White" />

                                </LinearGradientBrush>

                            </Border.Background>

                            <ContentPresenter

                                Margin="2"

                                HorizontalAlignment="Center"

                                VerticalAlignment="Center"

                                RecognizesAccessKey="True" />

                        </Border>

                        <ControlTemplate.Triggers>

                            <Trigger

                                Property="IsPressed"

                                Value="true">

                                <Setter

                                    TargetName="border"

                                    Property="Background">

                                    <Setter.Value>

                                        <LinearGradientBrush

                                            StartPoint="0 0.5"

                                            EndPoint="1 0.5">

                                            <GradientStop Offset="0.0" Color="{Binding Background.Color, RelativeSource={RelativeSource TemplatedParent}}" />

                                            <GradientStop Offset="0.9" Color="DarkSlateGray" />

                                        </LinearGradientBrush>

                                    </Setter.Value>

                                </Setter>

                            </Trigger>

                        </ControlTemplate.Triggers>

                    </ControlTemplate>

                </Setter.Value>

            </Setter>

        </Style>

    </Window.Resources>

    <Grid>

        <Button

            Margin="50"

            Background="RoyalBlue"

            FontWeight="Bold"

            FontSize="18">

            Test

        </Button>

    </Grid>

</Window>

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

Posted by 사용자 icodebroker

댓글을 달아 주세요