0

I have a treeview that displays a hierarchy of objects of type Company.

Company has (among others) a property Bool? Checked. I am using that value in the checkbox for each row. I would like the checkbox to (also) indicate if any childs has been selected, but Im not sure how to build the Getter for the Checked property.

I guess the problem is that the value does not only represent the current objects value, but also the combined value of the childs. Is it solvable or do I need to rethink?

Example of tristate tree

This is the outcome I would like to get:

  • Checked = True (if the item itself is checked)
  • Checked = False (if the item itself is not checked AND all childs/grandchilds are not checked)
  • Checked = Null (If the item itself is not checked and SOME childs/grandchilds are checked)
  • Checked = Null (if the item itself is not checked and ALL childs/grandchilds are checked)

Class Company:

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; }
    }
David
  • 1,601
  • 3
  • 22
  • 33
  • Can't you just enumerate the children in the getter and check their state? But maybe i don't get what you want to achieve. – Rev Sep 03 '13 at 12:02
  • Yes, I can enumerate through all childs. No problem. It gets tricky though because each child can have childs of its own. I think its the tristate-bool combined with a recursive function that makes me all confused. I simply cant figure out how to write a recursive function that returns a correct value! – David Sep 03 '13 at 12:06
  • I think there is a simple code solution to this involving XOR and some other stuff I yet has to master. But right now I end up with 60 lines of code that produces all other results imaginable except the correct one :) – David Sep 03 '13 at 12:08

1 Answers1

1

Ok, so using nullable bool is an OLV requirement right?

But shouldn't something this work to achieve what you want?

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; }
    }
}
Rev
  • 5,827
  • 4
  • 27
  • 51
  • haha, got an "StackOverflowException" when implementing this Getter! Kind of fun when u think about it :) – David Sep 03 '13 at 14:26
  • @David: That shouldn't happen. But looking at your original code example, you call "set { this.Checked = value; }" which would also cause a StackOverflow (no pun intended ;)) because you recursively call the property. Maybe you did the same mistake in the setter? – Rev Sep 03 '13 at 14:43
  • yep, missed it. I Called this.Marked instead of this._marked. Now its working. Big thx! – David Sep 04 '13 at 09:41