确定选定的三态

时间:2013-09-03 11:07:51

标签: c# recursion objectlistview tri-state-logic

我有一个树视图,显示Company类型的对象层次结构。

Company(以及其他)属性Bool? Checked。我在每行的复选框中使用该值。我希望复选框(也)指示是否已选择任何子项,但我不确定如何为Getter属性构建Checked

我想问题是该值不仅代表当前对象的值,还代表子代的组合值。它是可以解决的还是我需要重新思考?

Example of tristate tree

这是我想得到的结果:

  • Checked = True(如果项目本身已被选中)
  • Checked = False(如果 项目本身未被检查,所有孩子/孙子都没有 检查)
  • Checked = Null(如果未检查项目本身,则为SOME 孩子/孙子被检查)
  • Checked = Null(如果未检查项目本身且全部 孩子/孙子被检查)

班级公司:

public class Company
{
    public Company()
    {
        this.childs = new List<Company>();
    }
    public int ID { get; set; }
    public string Title { get; set; }
    public List<Company> childs { get; set; }
    public int NrOfChilds { get { return childs.Count; } }
    public bool Checked {
        get { ??? }
        set { this.Checked = value; }
    }

1 个答案:

答案 0 :(得分:1)

好的,所以使用可空的bool是一个OLV要求吗?

但这不应该能达到你想要的效果吗?

class Entity {
    private bool? _CheckState;
    public List<Entity> ChildEntities { get; set; }

    public Entity() {
        _CheckState = false;
        ChildEntities = new List<Entity>();
    }

    public bool? CheckState {
        get {
            if (_CheckState == true) {
                return true;
            } else if (ChildEntities.All(child => child.CheckState == false)) {
                return false;
            } else {
                return null;
            }
        }
        set { _CheckState = value; }
    }
}