■ 커스텀 로직에 근거한 템플리트를 선택하는 방법을 보여준다.
▶ RowDataTemplateSelector.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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 } } |