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

728x90
반응형

TestProject.zip
다운로드

▶ 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 x:Class="TestProject.EmployeeButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestProject">
    <Button.ContentTemplate>
        <DataTemplate DataType="{x:Type local:Employee}">
            <DockPanel>
                <Image DockPanel.Dock="Left"
                    Stretch="None"
                    Source="{Binding Path=Face}" />
                <UniformGrid
                    VerticalAlignment="Center"
                    Margin="10"
                    Rows="2">
                    <TextBlock
                        TextAlignment="Center"
                        FontSize="16"
                        Text="{Binding Path=Name}" />
                    <StackPanel
                        Orientation="Horizontal"
                        TextBlock.FontSize="16">
                        <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>

 

▶ EmployeeTemplateSelector.cs

using System.Windows;
using System.Windows.Controls;

namespace TestProject
{
    /// <summary>
    /// 직원 데이터 템플리트 셀렉터
    /// </summary>
    public class EmployeeDataTemplateSelector : DataTemplateSelector
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 템플리트 선택하기 - SelectTemplate(item, container)

        /// <summary>
        /// 템플리트 선택하기
        /// </summary>
        /// <param name="item">항목</param>
        /// <param name="container">컨테이너</param>
        /// <returns>데이터 템플리트</returns>
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            Employee employee = item as Employee;

            FrameworkElement frameworkElement = container as FrameworkElement;

            return (DataTemplate)frameworkElement.FindResource
            (
                employee.LeftHanded ? "LeftHandedDataTemplateKey" : "RightHandedDataTemplateKey"
            );
        }

        #endregion
    }
}

 

▶ 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"
    xmlns:local="clr-namespace:TestProject"
    Width="800"
    Height="600"
    Title="DataTemplateSelector 클래스 사용하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Window.Resources>
        <local:EmployeeDataTemplateSelector x:Key="EmployeeDataTemplateSelectorKey" />
        <DataTemplate x:Key="RightHandedDataTemplateKey">
            <DockPanel>
                <Image DockPanel.Dock="Right"
                    Stretch="None"
                    Source="{Binding Path=Face}" />
                <UniformGrid
                    Rows="2"
                    VerticalAlignment="Center"
                    Margin="10">
                    <TextBlock
                        TextAlignment="Center"
                        Text="{Binding Path=Name}" />
                    <StackPanel Orientation="Horizontal">
                        <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>
        <DataTemplate x:Key="LeftHandedDataTemplateKey">
            <DockPanel>
                <Image DockPanel.Dock="Left"
                    Stretch="None"
                    Source="{Binding Path=Face}" />
                <UniformGrid
                    Rows="2"
                    VerticalAlignment="Center"
                    Margin="10">
                    <TextBlock
                        TextAlignment="Center"
                        Text="{Binding Path=Name}" />
                    <StackPanel Orientation="Horizontal">
                        <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>
        <Style
            TargetType="{x:Type Button}">
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment"   Value="Center" />
            <Setter Property="Margin"              Value="12"     />
        </Style>
    </Window.Resources>
    <Grid>
        <ScrollViewer>
            <StackPanel
                HorizontalAlignment="Center"
                VerticalAlignment="Center">
                <Button ContentTemplateSelector="{StaticResource EmployeeDataTemplateSelectorKey}">
                    <Button.Content>
                        <local:Employee
                            Name="Betty"
                            BirthDate="1970/08/31"
                            Face="IMAGE/Betty.png"
                            LeftHanded="False" />
                    </Button.Content>
                </Button>
                <Button ContentTemplateSelector="{StaticResource EmployeeDataTemplateSelectorKey}">
                    <Button.Content>
                        <local:Employee
                            Name="Edgar"
                            BirthDate="1965/02/02"
                            Face="IMAGE/Edgar.png"
                            LeftHanded="True" />
                    </Button.Content>
                </Button>
                <Button ContentTemplateSelector="{StaticResource EmployeeDataTemplateSelectorKey}">
                    <Button.Content>
                        <local:Employee
                            Name="Sally"
                            BirthDate="1980/07/12"
                            Face="IMAGE/Sally.png"
                            LeftHanded="True" />
                    </Button.Content>
                </Button>
                <Button ContentTemplateSelector="{StaticResource EmployeeDataTemplateSelectorKey}">
                    <Button.Content>
                        <local:Employee
                            Name="Jim"
                            BirthDate="1975/06/15"
                            Face="IMAGE/Jim.png"
                            LeftHanded="False" />
                    </Button.Content>
                </Button>
            </StackPanel>
        </ScrollViewer>
    </Grid>
</Window>
728x90
반응형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요