/// <summary> /// Create a new instance of this class. /// </summary> public Submenu() { this.entries = new MenuEntryCollection { Parent = this }; this.Text = "Submenu"; }
private void move(bool up) { MenuEntryCollection parent = this.Parent; if (parent == null) { return; // Unable to do anything } int index = parent.IndexOf(this); // The item is at the beginning of the collection if ((index == 0 && up) || (index == parent.Count - 1 && !up)) { if (parent.Parent == null) { return; // We can't move the element any further. } MenuEntryCollection parentParent = parent.Parent.Parent; if (parentParent == null) { return; // There is nothing we can do. } int parentIndex = parentParent.IndexOf(parent.Parent); this.IsSelected = false; parent.Remove(this); if (up) { parentParent.Insert(parentIndex, this); } else { parentParent.Insert(parentIndex + 1, this); } this.IsSelected = true; } else // The element is somewhere in the parent collection and can be moved. { int delta = 1; // Increase the index by one. if (up) { delta = -1; // Decrease the index by one. } Submenu moveInto = parent[index + delta] as Submenu; if (moveInto == null) { parent.Move(index, index + delta); } else { this.IsSelected = false; parent.Remove(this); moveInto.IsExpanded = true; if (up) { moveInto.MenuEntries.Add(this); } else { moveInto.MenuEntries.Insert(0, this); } this.IsSelected = true; } } }