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

■ ControlTemplate 클래스 : Button 클래스 정의하기

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


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 클래스 : Button 클래스 정의하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Grid Name="grid">

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Windows;

using System.Windows.Controls;

using System.Windows.Media;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            ControlTemplate template = new ControlTemplate(typeof(Button));

 

            FrameworkElementFactory borderFactory = new FrameworkElementFactory(typeof(Border));

 

            borderFactory.Name = "border";

 

            borderFactory.SetValue(Border.BorderBrushProperty    , Brushes.Red                   );

            borderFactory.SetValue(Border.BorderThicknessProperty, new Thickness(3)              );

            borderFactory.SetValue(Border.BackgroundProperty     , SystemColors.ControlLightBrush);

 

            FrameworkElementFactory contentPresenterFactory = new FrameworkElementFactory(typeof(ContentPresenter));

 

            contentPresenterFactory.Name = "content";

 

            contentPresenterFactory.SetValue(ContentPresenter.ContentProperty, new TemplateBindingExtension(Button.ContentProperty));

            contentPresenterFactory.SetValue(ContentPresenter.MarginProperty , new TemplateBindingExtension(Button.PaddingProperty));

 

            borderFactory.AppendChild(contentPresenterFactory);

 

            template.VisualTree = borderFactory;

 

            Trigger trigger = new Trigger();

 

            trigger.Property = UIElement.IsMouseOverProperty;

            trigger.Value    = true;

 

            Setter setter = new Setter();

 

            setter.TargetName = "border";

            setter.Property   = Border.CornerRadiusProperty;

            setter.Value      = new CornerRadius(24);

 

            trigger.Setters.Add(setter);

 

            setter = new Setter();

 

            setter.Property = Control.FontStyleProperty;

            setter.Value    = FontStyles.Italic;

 

            trigger.Setters.Add(setter);

 

            template.Triggers.Add(trigger);

 

            trigger = new Trigger();

 

            trigger.Property = Button.IsPressedProperty;

            trigger.Value    = true;

 

            setter = new Setter();

 

            setter.TargetName = "border";

            setter.Property   = Border.BackgroundProperty;

            setter.Value      = SystemColors.ControlDarkBrush;

 

            trigger.Setters.Add(setter);

 

            template.Triggers.Add(trigger);

 

            Button button = new Button();

 

            button.Template = template;

 

            button.Content             = "커스텀 템플리트 버튼";

            button.Padding             = new Thickness(20);

            button.FontSize            = 48;

            button.HorizontalAlignment = HorizontalAlignment.Center;

            button.VerticalAlignment   = VerticalAlignment.Center;

 

            button.Click += button_Click;

 

            this.grid.Children.Add(button);

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 버튼 클릭시 처리하기 - button_Click(sender, e)

 

        /// <summary>

        /// 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void button_Click(object sender, RoutedEventArgs e)

        {

            MessageBox.Show(this, "INFORMATION", "버튼을 클릭했습니다.");

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요