问题描述
假设我有一个由教师和学生组成的 2 个组的 TreeList,有一列“状态”。我正在尝试实现一个按钮来选择所有活跃的教师和学生组。
这是我目前所拥有的
public class MatchStatusOps : TreeListOperation
{
private string fieldName;
private string status;
private TreeList helper;
public TreeListMatchStatusOperation(string fieldName,string status,TreeList helper)
{
this.fieldName = fieldName;
this.status = status;
this.helper = helper;
}
public override void Execute(TreeListNode node)
{
String statusValue = Convert.ToString(node[fieldName]);
if (statusValue.Equals(status))
helper.SetNodeCheckState(node,CheckState.Checked,true);
}
}
然后,我从我的 TreeList 类中调用它
MatchStatusOps operation = new MatchStatusOps("Status","Active",this);
this.NodesIterator.DoOperation(operation);
我无法选中复选框,我想可能是因为选中的节点是状态节点,而不是复选框节点?我有什么想法可以让它发挥作用吗?谢谢。
解决方法
我想可能是因为选择的节点是状态节点,而不是复选框节点
那些不是节点,那些是列(通常由字段名称绑定)。一个节点可以包含任意数量的列。
此外,这行代码可确保您的操作在所有节点上运行:
NodesIterator.DoOperation(operation);
以下代码基于对屏幕截图中填充 TreeList
(如果未绑定)的数据源或代码的有根据的猜测。
代码:
public class MatchStatusOps : TreeListOperation
{
private readonly string fieldName;
private readonly string status;
private readonly string checkboxFieldName;
public MatchStatusOps(string fieldName,string status,string checkboxFieldName)
{
this.fieldName = fieldName;
this.status = status;
this.checkboxFieldName = checkboxFieldName;
}
public override void Execute(TreeListNode node)
{
String statusValue = Convert.ToString(node[fieldName]);
if (statusValue.Equals(status))
node[checkboxFieldName] = true;
}
}
用法:
var operation = new MatchStatusOps("Status","Active","YourCheckboxFieldName");
NodesIterator.DoOperation(operation);
注意我在给定节点上设置了一个列来检查,而不是节点本身。
在撰写本文时,我已确认使用最新的 DevExpress 版本进行工作。