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

TestProject.zip
0.01MB

▶ Generic.xml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestProject">
    <Style x:Key="FocusVisualStyleKey">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Control}">
                    <Grid Margin="8">
                        <Ellipse Name="r1"
                            StrokeThickness="1"
                            Stroke="Black"
                            StrokeDashArray="2 2" />
                        <Border Name="border"
                            Width="{TemplateBinding ActualWidth}"
                            Height="{TemplateBinding ActualHeight}"
                            BorderThickness="1"
                            CornerRadius="2" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="{x:Type local:EllipseButton}" TargetType="{x:Type local:EllipseButton}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisualStyleKey}" />
        <Setter Property="Margin"           Value="2"         />
        <Setter Property="Background"       Value="#ff6db4ef" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>
                        <Ellipse Fill="{TemplateBinding Background}" />
                        <Ellipse>
                            <Ellipse.Fill>
                                <RadialGradientBrush>
                                    <GradientStop Offset="0"    Color="#00000000" />
                                    <GradientStop Offset="0.88" Color="#00000000" />
                                    <GradientStop Offset="1"    Color="#80000000" />
                                </RadialGradientBrush>
                            </Ellipse.Fill>
                        </Ellipse>
                        <Ellipse Name="highlightEllipse"
                            Margin="10">
                            <Ellipse.Fill >
                                <LinearGradientBrush >
                                    <GradientStop Offset="0"   Color="#50ffffff" />
                                    <GradientStop Offset="0.5" Color="#00ffffff" />
                                    <GradientStop Offset="1"   Color="#50ffffff" />
                                </LinearGradientBrush>
                            </Ellipse.Fill>
                        </Ellipse>
                        <ContentPresenter Name="contentPresenter"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter
                                TargetName="highlightEllipse"
                                Property="Fill">
                                <Setter.Value>
                                    <LinearGradientBrush
                                        StartPoint="0.3 0"
                                        EndPoint="0.7 1">
                                        <GradientStop Offset="0"   Color="#50ffffff" />
                                        <GradientStop Offset="0.5" Color="#00ffffff" />
                                        <GradientStop Offset="1"   Color="#50ffffff" />
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter
                                TargetName="contentPresenter"
                                Property="RenderTransform">
                                <Setter.Value>
                                    <TranslateTransform X="0.5" Y="0.5" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

 

728x90

 

▶ EllipseButton.cs

using System.Windows;
using System.Windows.Controls;

namespace TestProject
{
    /// <summary>
    /// 타원 버튼
    /// </summary>
    public class EllipseButton: Button
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Static

        #region 생성자 - EllipseButton()

        /// <summary>
        /// 생성자
        /// </summary>
        static EllipseButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata
            (
                typeof(EllipseButton),
                new FrameworkPropertyMetadata(typeof(EllipseButton))
            );
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 템플리트 적용시 처리하기 - OnApplyTemplate()

        /// <summary>
        /// 템플리트 적용시 처리하기
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            ContentPresenter contentPresenter = GetTemplateChild("contentPresenter") as ContentPresenter;

            contentPresenter.Margin = new Thickness(10);
        }

        #endregion
    }
}

 

반응형

 

▶ 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"
    xmlns:local="clr-namespace:TestProject"
    Width="800"
    Height="600"
    Title="FrameworkElement 클래스 : GetTemplateChild 메소드를 사용해 템플리트 내 자식 객체 구하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <local:EllipseButton
            Width="200"
            Height="200"
            Content="테스트" />
    </Grid>
</Window>
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요