Auto Collapse Groups when another one is expanded

Sep 6, 2011 at 4:12 PM

I would like to have only one group expanded at a time, i.e., each time the user expands one group, all siblings of this group are collapsed.

The only thing I could come up with is quite invasive:

First of all a method to check whether two items are siblings

---

        public static bool AreSiblings(RowIdentifier lhs, RowIdentifier rhs)
        {
            if (lhs == rhs)
                return false;

            if (lhs.GroupValues.Length != rhs.GroupValues.Length)
                return false;

            for (int i = 0; i < lhs.GroupValues.Length - 1; i++)
            {
                if (lhs.GroupValues[i] != rhs.GroupValues[i])
                    return false;
            }

            return true;
        }

---

(actually this method is only correct for GroupIdentifiers (the "Length - 1" statement), but thats the only situation I am using it).

The mouse click on the collapse button eventually calls SetGroupState. There I find all siblings of the clicked group

---

            List<RowIdentifier> removeItems = new List<RowIdentifier>();
            if (groupState == GroupState.Expanded && someFlagToActivateTheFeature)
            {
                foreach (var item in _groupExpansionState)
                {
                    if (AreSiblings(ri, item))
                    {
                        removeItems.Add(item);
                    }
                }
            }

---

and remove or add those items to _groupExpansionState afterwards

---

                if (_globalGroupState == groupState)
                {
                    _groupExpansionState.Remove(ri);
                    _groupExpansionState.AddRange(removeItems);
                }
                else
                {
                    _groupExpansionState.Add(ri);
                    _groupExpansionState.RemoveRange(removeItems);
                }

---

Any ideas for a less invasive version?

In particular, I wanted to use ListControl.GetGroupedState. Unfortunately, there I have a RowIdentifier for which I must more or less find out if it is currently being clicked, while the OnMouseDown is handled by a GroupSection.