HierarchicalDataTemplate-GridControl

xaml

<Window.DataContext>
    <local:HierarchicalWindowViewModel/>
</Window.DataContext>
<Window.Resources>
    <HierarchicalDataTemplate x:Key="gradetemp" DataType="{x:Type local:Grade}" ItemsSource="{Binding Students}">
        <dx:MeasurePixelSnapper>
            <StackPanel Orientation="Vertical">
                <Label Content="{Binding Row.GradeId}"/>
            </StackPanel>
        </dx:MeasurePixelSnapper>
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate x:Key="stutemp" DataType="{x:Type local:Student}" ItemsSource="{Binding StuScore}">
        <dx:MeasurePixelSnapper>
            <StackPanel Orientation="Vertical">
                <Label Content="{Binding Row.StuName}"/>
            </StackPanel>
        </dx:MeasurePixelSnapper>
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate x:Key="subtemp" DataType="{x:Type local:Subject}" ItemsSource="{Binding .}">
        <dx:MeasurePixelSnapper>
            <StackPanel Orientation="Horizontal">
                <Label Content="{Binding Row.Subj}"/>
                <Label Content="{Binding Row.Score}"/>
            </StackPanel>
        </dx:MeasurePixelSnapper>
    </HierarchicalDataTemplate>

    <local:SpamRuleHierarchicalDataTemplateSelector x:Key="templateSelector" 
            GradeTemplate="{StaticResource gradetemp}" 
            StuTemplate="{StaticResource stutemp}" 
            SubTemplate="{StaticResource subtemp}" />
</Window.Resources>
<Grid>
    <dxg:GridControl x:Name="treeList" ItemsSource="{Binding Grades}" >
        <dxg:GridControl.View>
            <dxg:TreeListView x:Name="view" TreeDerivationMode="HierarchicalDataTemplate"
                                DataRowTemplateSelector="{StaticResource templateSelector}"
                                AllowEditing="True" NavigationStyle="Cell" />
        </dxg:GridControl.View>
    </dxg:GridControl>
</Grid>

model.cs

class HierarchicalWindowViewModel
{
    public List<Grade> Grades { get; set; }

    public HierarchicalWindowViewModel()
    {
        Grades = new List<Grade>();
        List<Student> students = new List<Student>();
        for (int i = 0; i < 20; i++)
        {
            students.Add(new Student
            {
                StuName = $"Name{i}",
                StuScore = new List<Subject>
                {
                    new Subject{Subj = "math", Score = 66},
                    new Subject{Subj = "cs", Score = 77},
                }
            }); ;
        }

        for (int i = 0; i < 5; i++)
        {
            Grades.Add(new Grade
            {
                GradeId = $"Grade{i}",
                Students = students
            });
        }
    }
}

class Grade
{
    public string GradeId { get; set; }
    public List<Student> Students { get; set; }
}

class Student
{
    public string StuName { get; set; }
    public List<Subject> StuScore { get; set; }
}

class Subject
{
    public string Subj { get; set; }
    public int Score { get; set; }
}

class SpamRuleHierarchicalDataTemplateSelector : DataTemplateSelector
{
    public HierarchicalDataTemplate GradeTemplate { get; set; }
    public HierarchicalDataTemplate StuTemplate { get; set; }
    public HierarchicalDataTemplate SubTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        RowData treeRowData = item as RowData;
        if (treeRowData == null || treeRowData.Row == null)
        {
            return null;
        }

        if (treeRowData.Row is Grade)
        {
            return GradeTemplate;
        }

        if (treeRowData.Row is Student)
        {
            return StuTemplate;
        }

        if (treeRowData.Row is Subject)
        {
            return SubTemplate;
        }

        return null;
    }
}

 

posted @ 2021-12-24 15:24  Kyle0418  阅读(65)  评论(2编辑  收藏  举报