■ 컨트롤 템플리트에서 컨트롤 참조 구하기

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

MainWindow.xaml.cs

 

 

<Window x:Class="TestProject.MainWindow"

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

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

    Width="600"

    Height="450"

    Title="컨트롤 템플리트에서 컨트롤 참조 구하기">

    <Window.Resources>

        <ControlTemplate x:Key="ButtonControlTemplateKey" TargetType="Button">

            <Grid>

                <Rectangle x:Name="rectangle"

                    Stroke="Black"

                    StrokeThickness="3"

                    RadiusX="10"

                    RadiusY="10"

                    Fill="Orange">

                </Rectangle>

                <Viewbox

                    Margin="{TemplateBinding Padding}"

                    VerticalAlignment="Center"

                    HorizontalAlignment="Center">

                    <ContentPresenter />

                </Viewbox>

            </Grid>

        </ControlTemplate>

    </Window.Resources>  

    <Grid>

        <Button

            Name="button"

            HorizontalAlignment="Center"

            VerticalAlignment="Center"

            Width="280"

            Height="80"

            Template="{StaticResource ButtonControlTemplateKey}"

            Padding="10"

            Content="ChangeColor"

            Click="button_Click" />

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System;

using System.Windows;

using System.Windows.Media;

using System.Windows.Shapes;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

        }

 

        #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)

        {

            try

            {

                Rectangle rectangle = this.button.Template.FindName("rectangle", button) as Rectangle;

 

                rectangle.Fill = Brushes.Red;

            }

            catch(Exception exception)

            {

                MessageBox.Show(exception.Message);   

            }

        }

 

        #endregion

    }

}

 

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

※ 테스트 결과 컨트롤이 로드되기 전에는 참조를 구할 수 없었다.

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요