TreeListControl中显示列的样式+MVVM架构

https://download.csdn.net/download/tiz198183/10956891

下载地址 https://download.csdn.net/download/tiz198183/10956891 

https://blog.csdn.net/xchicken/article/details/19537679

    public class PropertyChangedBase:INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        public virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public enum SexOption
    {
        Male,
        Female
    };
    public class Student
    {
        public int StudentId { get; set; }
        public SexOption StudentGender { get; set; }
        public string StudentName { get; set; }
        public int  StudentAge { get; set; }
        public string StudentAddress { get; set; }
        public int TeacherId { get; set; }
        public string TeacherName { get; set; }

    }
    public class Students
    {
        private readonly ObservableCollection<Student> _list = new ObservableCollection<Student>();
        public Students()
        {
            int nTeacher;
            SexOption so;
            //插入20个学生记录
            for (int i = 0; i < 20; i++)
            {
                nTeacher = i / 5;
                if (i % 2 == 0)
                {
                    so = SexOption.Male;
                }
                else
                {
                    so = SexOption.Female;
                }
                _list.Add(new Student
                {
                    StudentId = i,
                    StudentGender = so,
                    StudentName = "Student" + i.ToString(),
                    StudentAge = i,
                    StudentAddress = "地球",
                    TeacherId = nTeacher,//每5个学生分配同一个老师
                    TeacherName = "Teacher" + nTeacher.ToString()
                });
            }
        }

        public ObservableCollection<Student> GetStudents()
        {
            return (_list != null) ? _list : null;
        }
    }
    public class StudentViewModel:PropertyChangedBase
    {
        private ObservableCollection<Student> _studentList;
        private readonly Students _students = new Students();

        public ObservableCollection<Student> StudentListModel
        {
            get { return _studentList; }
            set
            {
                _studentList = value;
                OnPropertyChanged("StudentListModel");
            }
        }
        public StudentViewModel()
        {
            _studentList = _students.GetStudents();
        }
    }
    public partial class TreeList : Window
    {
        private readonly StudentViewModel _vm = new StudentViewModel();
        public TreeList()
        {
            InitializeComponent();
            TreeList1.ItemsSource = _vm.StudentListModel;
        }
    }
<Window x:Class="WpfApplication6.View.TreeList"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:model="clr-namespace:WpfApplication6.Model"
        Title="TreeList" Height="300" Width="300">
    <Window.Resources>
        <ObjectDataProvider x:Key="DataProvider1" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type Type="model:SexOption"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
    <Grid>
        <dxg:TreeListControl x:Name="TreeList1">
            <dxg:TreeListControl.Columns>
                <dxg:TreeListColumn FieldName="TeacherName" Header="辅导老师"/>
                <dxg:TreeListColumn FieldName="StudentName" Header="学生姓名"/>
                <dxg:TreeListColumn FieldName="StudentGender" Header="学生性别">
                    <dxg:TreeListColumn.EditSettings>
                        <dxe:ComboBoxEditSettings ItemsSource="{Binding Source={StaticResource DataProvider1}}" IsTextEditable="False"/>
                    </dxg:TreeListColumn.EditSettings>
                </dxg:TreeListColumn>
                <dxg:TreeListColumn FieldName="StudentAge" Header="学生年龄"/>
                <dxg:TreeListColumn FieldName="StudentAddress" Header="学生住址"/>
            </dxg:TreeListControl.Columns>
            <dxg:TreeListControl.View>
                <dxg:TreeListView AutoWidth="True" ParentFieldName="TeacherId" KeyFieldName="StudentId" EditorButtonShowMode="ShowAlways"/>
            </dxg:TreeListControl.View>
        </dxg:TreeListControl>
        
    </Grid>
</Window>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值