■ DataTemplate 엘리먼트 : Triggers 속성 사용하기

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

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

    }

}

 

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"

    Width="800"

    Height="600"

    Title="데이터 템플리트 트리거 사용하기">

    <Window.Resources>

        <DataTemplate x:Key="ButtonDataTemplateKey">

            <DockPanel>

                <Image

                    Name="image"

                    DockPanel.Dock="Left"

                    Stretch="None"

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

                <UniformGrid

                    Rows="2"

                    VerticalAlignment="Center"

                    Margin="12">

                    <TextBlock

                        FontSize="16pt"

                        TextAlignment="Center"

                        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.Triggers>

                <DataTrigger Binding="{Binding Path=LeftHanded}" Value="False">

                    <Setter TargetName="image" Property="DockPanel.Dock" Value="Right" />

                </DataTrigger>

            </DataTemplate.Triggers>

        </DataTemplate>

        <Style

            TargetType="{x:Type Button}">

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

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

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

        </Style>

    </Window.Resources>

    <StackPanel>

        <Button ContentTemplate="{StaticResource ButtonDataTemplateKey}">

            <Button.Content>

                <src:Employee

                    Name="Betty"

                    BirthDate="8/31/1970"

                    Face="Betty.png"

                    LeftHanded="False"/>

            </Button.Content>

        </Button>

        <Button ContentTemplate="{StaticResource ButtonDataTemplateKey}">

            <Button.Content>

                <src:Employee

                    Name="Edgar"

                    BirthDate="2/2/1965"

                    Face="Edgar.png"

                    LeftHanded="True"/>

            </Button.Content>

        </Button>

        <Button ContentTemplate="{StaticResource ButtonDataTemplateKey}">

            <Button.Content>

                <src:Employee

                    Name="Sally"

                    BirthDate="7/12/1980"

                    Face="Sally.png"

                    LeftHanded="True"/>

            </Button.Content>

        </Button>

        <Button ContentTemplate="{StaticResource ButtonDataTemplateKey}">

            <Button.Content>

                <src:Employee

                    Name="Jim"

                    BirthDate="6/15/1975"

                    Face="Jim.png"

                    LeftHanded="False"/>

            </Button.Content>

        </Button>

    </StackPanel>

</Window>

 

MainWindow.xaml.cs

 

using System.Windows;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

        }

 

        #endregion

    }

}

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

 

Posted by 사용자 icodebroker

댓글을 달아 주세요