■ ControlTemplate 클래스 사용하기

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

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Media;

 

/// <summary>

/// 메인 윈도우

/// </summary>

public class MainWindow : Window

{

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

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

 

    #region 생성자 - MainWindow()

 

    /// <summary>

    /// 생성자

    /// </summary>

    public MainWindow()

    {

        Title = "버튼 팩토리 만들기";

 

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

 

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

 

        borderFrameworkElementFactory.Name = "border";

 

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

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

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

 

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

 

        contentFrameworkElementFactory.Name = "content";

 

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

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

 

        borderFrameworkElementFactory.AppendChild(contentFrameworkElementFactory);

 

        controlTemplate.VisualTree = borderFrameworkElementFactory;

 

        Trigger trigger = new Trigger();

 

        trigger.Property = UIElement.IsMouseOverProperty;

        trigger.Value    = true;

 

        Setter setter = new Setter();

 

        setter.Property   = Border.CornerRadiusProperty;

        setter.Value      = new CornerRadius(24);

        setter.TargetName = "border";

 

        trigger.Setters.Add(setter);

 

 

        setter = new Setter();

 

        setter.Property = Control.FontStyleProperty;

        setter.Value    = FontStyles.Italic;

 

        trigger.Setters.Add(setter);

 

        controlTemplate.Triggers.Add(trigger);

 

        trigger = new Trigger();

 

        trigger.Property = Button.IsPressedProperty;

        trigger.Value    = true;

 

        setter = new Setter();

 

        setter.Property   = Border.BackgroundProperty;

        setter.Value      = SystemColors.ControlDarkBrush;

        setter.TargetName = "border";

 

        trigger.Setters.Add(setter);

 

        controlTemplate.Triggers.Add(trigger);

 

        Button button = new Button();

 

        button.Template = controlTemplate;

 

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

        button.Padding             = new Thickness(20);

        button.FontSize            = 48;

        button.HorizontalAlignment = HorizontalAlignment.Center;

        button.VerticalAlignment   = VerticalAlignment.Center;

 

        button.Click += button_Click;

 

        Content = button;

    }

 

    #endregion

 

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

    ////////////////////////////////////////////////////////////////////////////////////////// Statis

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

 

    #region 프로그램 시작하기 - Main()

 

    /// <summary>

    /// 프로그램 시작하기

    /// </summary>

    [STAThread]

    public static void Main()

    {

        Application application = new Application();

 

        application.Run(new MainWindow());

    }

 

    #endregion

 

    ////////////////////////////////////////////////////////////////////////////////////////// Instance

    //////////////////////////////////////////////////////////////////////////////// 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("버튼을 클릭했습니다.", Title);

    }

 

    #endregion

}

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

Posted by 사용자 icodebroker

댓글을 달아 주세요