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>