• HierarchicalDataTemplateGridControl


    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;
        }
    }
    把圈子变小,把语言变干净,把成绩往上提,把故事往心里收,现在想要的以后你都会有。
  • 相关阅读:
    面试官:重写 equals 时为什么一定要重写 hashCode?
    MyBatis 中为什么不建议使用 where 1=1?
    面试官:方法重写时需要注意哪些问题?
    Java中List排序的3种方法
    面试官:this和super有什么区别?this能调用到父类吗?
    面试官:int和Integer有什么区别?为什么要有包装类?
    HashMap 中的一个“坑”!
    Java 中接口和抽象类的 7 大区别!
    List 去重的 6 种方法,这个方法最完美!
    面试官:如何实现 List 集合去重?
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/15727591.html
Copyright © 2020-2023  润新知