첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ ControlTemplate 엘리먼트에서 RelativeSource 태그 확장의 TemplatedParent 속성을 사용하는 방법을 보여준다.

TestProject.zip
다운로드

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="800"
    Height="600"
    Title="ControlTemplate 엘리먼트 : RelativeSource 태그 확장에서 TemplatedParent 모드 사용하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <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
            Width="300"
            Height="300"
            Background="RoyalBlue"
            FontWeight="Bold"
            FontSize="18">
            <TextBlock FontSize="48">
                테스트
            </TextBlock>
        </Button>
    </Grid>
</Window>
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요