첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

▶ C#

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

/// <summary>
/// 커스텀 데이터 템플리트 셀렉터
/// </summary>
public class CustomDataTemplateSelector : DataTemplateSelector
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Property
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 프로젝트 데이터 템플리트 - ProjectDataTemplate

    /// <summary>
    /// 프로젝트 데이터 템플리트
    /// </summary>
    public HierarchicalDataTemplate ProjectDataTemplate { get; set; }

    #endregion

    #region 프로젝트 스테이지 데이터 템플리트

    /// <summary>
    /// 프로젝트 스테이지 데이터 템플리트
    /// </summary>
    public HierarchicalDataTemplate ProjectStageDataTemplate { get; set; }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property
    ////////////////////////////////////////////////////////////////////////////////////////// Public

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

    /// <summary>
    /// 템플리트 선택하기
    /// </summary>
    /// <param name="item">항목</param>
    /// <param name="containerDependencyObject">컨테이너 의존 객체</param>
    /// <returns>DataTemplate 객체</returns>
    public override DataTemplate SelectTemplate(object item, DependencyObject containerDependencyObject)
    {       
        if(item is ProjectObject)
        {
            return ProjectDataTemplate;
        }

        if(item is ProjectStage)
        {
            return ProjectStageDataTemplate;
        }

        return null;
    }

    #endregion
}

 

728x90

 

▶ XAML

<Window.Resources>
    <ResourceDictionary>
        <local:CustomDataTemplateSelector x:Key="CustomDataTemplateSelectorKey">
            <local:CustomDataTemplateSelector.ProjectDataTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Path=ProjectStageCollection}" />
            </local:CustomDataTemplateSelector.ProjectDataTemplate>
            <local:CustomDataTemplateSelector.ProjectStageDataTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Path=TaskCollection}" />
            </local:CustomDataTemplateSelector.ProjectStageDataTemplate>
        </local:CustomDataTemplateSelector>
    </ResourceDictionary>
</Window.Resources>

...

<Grid xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid">
    <dxg:TreeListControl x:Name="treeListControl">
        <dxg:TreeListControl.Columns>
            <dxg:TreeListColumn
                FieldName="Name"
                AllowSorting="True" />
            <dxg:TreeListColumn
                FieldName="Executor"
                AllowSorting="True" />
        </dxg:TreeListControl.Columns>
        <dxg:TreeListControl.View>
            <dxg:TreeListView x:Name="treeListView"
                DataRowTemplateSelector="{StaticResource CustomDataTemplateSelectorKey}" 
                TreeDerivationMode="HierarchicalDataTemplate" />
        </dxg:TreeListControl.View>
    </dxg:TreeListControl>
</Grid>

※ ProjectObject, ProjectStage, Task 클래스는 'TreeListControl 클래스 : HierarchicalDataTemplate를 통해 트리 구축하기'를 참조한다.

728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요