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

728x90
반응형

TestProject.zip
다운로드

▶ NumberDataTemplateSelector.cs

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

namespace TestProject
{
    /// <summary>
    /// 번호 데이터 템플리트 셀렉터
    /// </summary>
    public class NumberDataTemplateSelector : DataTemplateSelector
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 번호 템플리트 - NumberTemplate

        /// <summary>
        /// 번호 템플리트
        /// </summary>
        public DataTemplate NumberTemplate { get; set; }

        #endregion
        #region 큰 번호 템플리트 - LargeNumberTemplate

        /// <summary>
        /// 큰 번호 템플리트
        /// </summary>
        public DataTemplate LargeNumberTemplate { get; set; }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// 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)
        {
            if(item == null)
            {
                return null;
            }

            int number = Convert.ToInt32((string)item);
            
            if(number < 6)
            {
                return NumberTemplate;
            }
            else
            {
                return LargeNumberTemplate;
            }
        }

        #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>
        <DataTemplate x:Key="NumberDataTemplateKey">
            <Grid>
                <Rectangle Stroke="Black" />
                <TextBlock
                    Margin="5"
                    FontSize="18"
                    Text="{Binding}" />
            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="LargeNumberDataTemplateKey">
            <Grid>
                <Ellipse
                    Stroke="Green"
                    StrokeThickness="4" />
                <TextBlock
                    Margin="10"
                    Foreground="Red"
                    FontSize="24" 
                    FontWeight="Bold"
                    Text="{Binding}" />
            </Grid>
        </DataTemplate>
        <local:NumberDataTemplateSelector x:Key="NumberDataTemplateSelectorKey"
            NumberTemplate="{StaticResource NumberDataTemplateKey}"
            LargeNumberTemplate="{StaticResource LargeNumberDataTemplateKey}" />
    </Window.Resources>
    <StackPanel
        HorizontalAlignment="Center"
        VerticalAlignment="Center">
        <Label
            HorizontalAlignment="Center"
            Margin="10"
            Foreground="Black"
            Content="{Binding ElementName=comboBox, Path=SelectedItem.Content}"
            ContentTemplateSelector="{StaticResource NumberDataTemplateSelectorKey}">
        </Label>
        <ComboBox Name="comboBox"
            HorizontalAlignment="Center"
            Width="100"
            Height="25"
            Margin="10">
            <ComboBoxItem>1</ComboBoxItem>
            <ComboBoxItem>2</ComboBoxItem>
            <ComboBoxItem>3</ComboBoxItem>
            <ComboBoxItem>4</ComboBoxItem>
            <ComboBoxItem>5</ComboBoxItem>
            <ComboBoxItem>6</ComboBoxItem>
            <ComboBoxItem IsSelected="True">7</ComboBoxItem>
            <ComboBoxItem>8</ComboBoxItem>
            <ComboBoxItem>9</ComboBoxItem>
            <ComboBoxItem>10</ComboBoxItem>
        </ComboBox>
    </StackPanel>
</Window>
728x90
반응형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요