■ DataTemplate 엘리먼트 사용하기 예제

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

MainWindow.xaml

 

<Window

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

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

    xmlns:src="clr-namespace:TestProject"

    x:Class="TestProject.MainWindow"

    Title="Content 속성 테스트">

    <Window.Resources>

        <Style TargetType="{x:Type src:EmployeeButton}">

            <Setter Property="HorizontalAlignment" Value="Center" />

            <Setter Property="VerticalAlignment"   Value="Center" />

            <Setter Property="Margin"              Value="12"     />

        </Style>

    </Window.Resources>

    <StackPanel

        Name="stackPanel"

        Button.Click="employeeButton_Click">

        <src:EmployeeButton>

            <src:EmployeeButton.Content>

                <src:Employee

                    Name="Betty"

                    BirthDate="8/31/1970"

                    Face="Betty.png" />

            </src:EmployeeButton.Content>

        </src:EmployeeButton>

        <src:EmployeeButton>

            <src:EmployeeButton.Content>

                <src:Employee

                    Name="Edgar"

                    BirthDate="2/2/1965"

                    Face="Edgar.png"/>

            </src:EmployeeButton.Content>

        </src:EmployeeButton>

        <src:EmployeeButton>

            <src:EmployeeButton.Content>

                <src:Employee

                    Name="Sally"

                    BirthDate="7/12/1980"

                    Face="Sally.png"/>

            </src:EmployeeButton.Content>

        </src:EmployeeButton>

    </StackPanel>

</Window>

 

MainWindow.xaml.cs

 

using System;

using System.Windows;

using System.Windows.Controls;

 

namespace WpfApplication1

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

 

            EmployeeButton employeeButton = new EmployeeButton();

 

            employeeButton.Content = new Employee("Jim", "Jim.png", new DateTime(1975, 6, 15), false);

 

            this.stackPanel.Children.Add(employeeButton);

        }

 

        #endregion

 

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

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

 

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

 

        /// <summary>

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

        /// </summary>

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

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

        private void employeeButton_Click(object sender, RoutedEventArgs e)

        {

            Button button = e.Source as Button;

 

            Employee employee = button.Content as Employee;

 

            MessageBox.Show(employee.Name + " button clicked!", Title);

        }

 

        #endregion

    }

}

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

 

■ DataTemplate 엘리먼트 사용하기

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

Employee.cs

 

using System;

 

namespace TestProject

{

    /// <summary>

    /// 임직원

    /// </summary>

    public class Employee

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

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

 

        #region Field

 

        /// <summary>

        /// 성명

        /// </summary>

        private string name;

 

        /// <summary>

        /// 얼굴

        /// </summary>

        private string face;

 

        /// <summary>

        /// 생일

        /// </summary>

        private DateTime birthDate;

 

        /// <summary>

        /// 왼손잡이 여부

        /// </summary>

        private bool leftHanded;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

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

 

        #region 성명 - Name

 

        /// <summary>

        /// 성명

        /// </summary>

        public string Name

        {

            set

            {

                this.name = value;

            }

            get

            {

                return this.name;

            }

        }

 

        #endregion

 

        #region 얼굴 - Face

 

        /// <summary>

        /// 얼굴

        /// </summary>

        public string Face

        {

            set

            {

                this.face = value;

            }

            get

            {

                return this.face;

            }

        }

 

        #endregion

 

        #region 생일 - BirthDate

 

        /// <summary>

        /// 생일

        /// </summary>

        public DateTime BirthDate

        {

            set

            {

                this.birthDate = value;

            }

            get

            {

                return this.birthDate;

            }

        }

 

        #endregion

 

        #region 왼손잡이 여부 - LeftHanded

 

        /// <summary>

        /// 왼손잡이 여부

        /// </summary>

        public bool LeftHanded

        {

            set

            {

                leftHanded = value;

            }

            get

            {

                return leftHanded;

            }

        }

 

        #endregion

 

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

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

 

        #region 생성자 - Employee()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public Employee()

        {

        }

 

        #endregion

 

        #region 생성자 - Employee(name, face, birthDate, leftHanded)

 

        /// <summary>

        /// 생성자

        /// </summary>

        /// <param name="name">성명</param>

        /// <param name="face">얼굴</param>

        /// <param name="birthDate">생일</param>

        /// <param name="leftHanded">왼손잡이 여부</param>

        public Employee(string name, string face, DateTime birthDate, bool leftHanded)

        {

            Name       = name;

            Face       = face;

            BirthDate  = birthDate;

            LeftHanded = leftHanded;

        }

 

        #endregion

    }

}

 

EmployeeButton.xaml

<Button

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

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

    xmlns:src="clr-namespace:TestProject"

    x:Class="TestProject.EmployeeButton">

    <Button.ContentTemplate>

        <DataTemplate DataType="{x:Type src:Employee}">

            <DockPanel>

                <Image

                    DockPanel.Dock="Left"

                    Stretch="None"

                    Source="{Binding Path=Face}" />

                <UniformGrid

                    VerticalAlignment="Center"

                    Margin="12"

                    Rows="2">

                    <TextBlock

                        TextAlignment="Center"

                        FontSize="16pt"

                        Text="{Binding Path=Name}" />

                    <StackPanel

                        Orientation="Horizontal"

                        TextBlock.FontSize="12pt">

                            <TextBlock Text="{Binding Path=BirthDate.Month}" />

                            <TextBlock Text="/" />

                            <TextBlock Text="{Binding Path=BirthDate.Day}" />

                            <TextBlock Text="/" />

                            <TextBlock Text="{Binding Path=BirthDate.Year}" />

                    </StackPanel>

                </UniformGrid>

            </DockPanel>

        </DataTemplate>

    </Button.ContentTemplate>

</Button>

 

EmployeeButton.xaml.cs

 

using System.Windows.Controls;

 

namespace TestProject

{

    /// <summary>

    /// 직원 버튼

    /// </summary>

    public partial class EmployeeButton : Button

    {

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

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

 

        #region 생성자 - EmployeeButton()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public EmployeeButton()

        {

            InitializeComponent();

        }

 

        #endregion

    }

}

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

 

Posted by 사용자 icodebroker

댓글을 달아 주세요