첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ RowDataTemplateSelector.cs

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

using DevExpress.Xpf.Grid;

namespace TestProject
{
    /// <summary>
    /// 행 데이터 템플리트 선택자
    /// </summary>
    public class RowDataTemplateSelector : DataTemplateSelector
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

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

        /// <summary>
        /// 템플리트 선택하기
        /// </summary>
        /// <param name="item">항목</param>
        /// <param name="dependencyObject">종속 객체</param>
        /// <returns>데이터 템플리트</returns>
        public override DataTemplate SelectTemplate(object item, DependencyObject dependencyObject)
        {
            RowData          rowData                   = item             as RowData;
            FrameworkElement containerFrameworkElement = dependencyObject as FrameworkElement;

            if(rowData != null && containerFrameworkElement != null)
            {
                return rowData.EvenRow ? containerFrameworkElement.FindResource("EvenRowDataTemplateKey") as DataTemplate :
                    containerFrameworkElement.FindResource("OddRowDataTemplateKey") as DataTemplate;
            }

            return base.SelectTemplate(item, dependencyObject);
        }

        #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"
    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
    Width="800"
    Height="600"
    Title="GridControl 클래스 : 커스텀 로직에 근거한 템플리트 선택하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Window.Resources>
        <local:RowDataTemplateSelector x:Key="RowDataTemplateSelectorKey" />
        <DataTemplate x:Key="EvenRowDataTemplateKey">
            <Border
                Margin="3"
                CornerRadius="5"
                Background="Blue">
                <TextBlock
                    Margin="5"
                    Foreground="White"
                    Text="{Binding DataContext.IssueName}" />
            </Border>
        </DataTemplate>
        <DataTemplate x:Key="OddRowDataTemplateKey">
            <Border
                Margin="3"
                CornerRadius="5"
                Background="Orange">
                <TextBlock
                    Margin="5"
                    Foreground="White"
                    Text="{Binding DataContext.IssueName}" />
            </Border>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <dxg:GridControl x:Name="gridControl"
            AutoGenerateColumns="AddNew">
            <dxg:GridControl.View>
                <dxg:TableView
                    ShowIndicator="False"
                    ShowGroupPanel="False"
                    ShowColumnHeaders="False"
                    ShowHorizontalLines="False"
                    DataRowTemplateSelector="{StaticResource RowDataTemplateSelectorKey}" />
            </dxg:GridControl.View>
        </dxg:GridControl>
    </Grid>
</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();

            this.gridControl.ItemsSource = new IssueList().GetData();
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요