引言
PropertyGridControl
实际是Vertical Grid,它的每一行对应的是PropertyGridControl.SelectedObject中的属性,如果我想根据值隐藏行应该怎么做呢?
解决方案
假如我现在只需要显示Course.Id=2
的DicClassRoomId_Name
栏位,查看Tree Traversal可知,可以通过VGridRowsIterator.DoOperation 方法遍历整个树达到自定义行内容的功能。
为此需要实现一个RowOperation (完整代码在文末)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class SetDicRowVisibleRowOperation : DevExpress.XtraVerticalGrid.Rows.RowOperation { public override void Execute(DevExpress.XtraVerticalGrid.Rows.BaseRow row) { Console.WriteLine(row.Properties.Caption); if (row.Tag is Course course) { var fieldName = nameof(Student.ListCourses) + "." + course.IndexName + "." + nameof(Course.DicClassRoomId_Name); if (row.Properties.FieldName == fieldName) row.Visible = course.Id == 2; } } }
|
在遍历之前,还需为Row保存相关信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| private void Form1_Load(object sender, EventArgs e) { var student = GetTestStudent(); propertyGridControl1.SelectedObject = student; propertyGridControl1.OptionsBehavior.PropertySort = DevExpress.XtraVerticalGrid.PropertySort.NoSort; propertyGridControl1.ExpandAllRows();
student.ListCourses.ForEach(x => { var fieldName = nameof(Student.ListCourses) + "." + x.IndexName + "." + nameof(Course.DicClassRoomId_Name); var row = propertyGridControl1.GetRowByFieldName(fieldName); row.Tag = x; });
propertyGridControl1.RowsIterator.DoOperation(new SetDicRowVisibleRowOperation()); }
|
由于上一篇文章devexpress如何在PropertyGridControl中直接编辑集合成员的影响,FieldName
发生了变化,此处根据PropertyDescriptor
的逻辑生成了FieldName
。
参考资料