public NestedMenu() { DataContext = this; Nodes = new ObservableCollection<AbstractNode>(); Path = new ObservableCollection<Node>(); InitializeComponent(); var r1 = new Node() { Name = "Root 1" }; r1.Children.Add(new Leaf() { Name = "Leaf", Parent=r1 }); r1.Children.Add(new Node() { Name = "Node 1", Parent=r1 }); var r2 = new Node() { Name = "Root 2" }; r2.Children.Add(new Leaf() { Name = "Leaf 2", Parent=r2 }); r2.Children.Add(new Node() { Name = "Node 3", Parent=r2 }); Nodes.Add(r1); Nodes.Add(r2); var megaroot = new Node() { Name = "Home" }; megaroot.Children.Add(r1); megaroot.Children.Add(r2); r1.Parent = megaroot; r2.Parent = megaroot; SelectedNode = megaroot; Path.Add(megaroot); }
public void Back() { if (SelectedNode == null || SelectedNode.Parent == null) return; Path.Remove(SelectedNode); SelectedNode = SelectedNode.Parent as Node; }
public void Navigate(AbstractNode destinationNode) { Node node = destinationNode as Node; Leaf leaf = destinationNode as Leaf; if (node != null) { Path.Add(destinationNode); SelectedNode = node; } if (leaf != null) { // NavigationService invoke } }
public DynamicNestedMenu2() { Nodes = new ObservableCollection<AbstractNode>(); Path = new ObservableCollection<AbstractNode>(); //SelectedNode = var r1 = new Node() { Name = "Root 1" }; r1.Children.Add(new Leaf() { Name = "Leaf" }); r1.Children.Add(new Node() { Name = "Node 1" }); var r2 = new Node() { Name = "Root 2" }; r2.Children.Add(new Leaf() { Name = "Leaf 2" }); r2.Children.Add(new Node() { Name = "Node 3" }); Nodes.Add(r1); Nodes.Add(r2); SelectedNode = new Node() { Name = "MegaRoot" }; SelectedNode.Children.Add(r1); SelectedNode.Children.Add(r2); }
private static void NodesChanged(object sender, DependencyPropertyChangedEventArgs e) { var oldValue = e.OldValue as ObservableCollection<AbstractNode>; if(oldValue!=null) oldValue.CollectionChanged -= CollectionChanged; var newValue = e.NewValue as ObservableCollection<AbstractNode>; if(newValue!=null) newValue.CollectionChanged += CollectionChanged; var control = sender as NestedMenu; if (control != null && control.Path != null) { var root = new Node() { Name = "Home" }; foreach (var node in newValue) root.Children.Add(node); control.Path.Clear(); control.SelectedNode = root; control.Path.Add(root); } }
public bool IsAnchestor(Node anchestor, Node node) { var parent = node.Parent as Node; if (parent != null && parent == anchestor) return true; else if (node.Parent == null) return false; else return IsAnchestor(anchestor, parent); }
public void Back(Node destinationNode) { foreach (var node in Path.ToList()) if (IsAnchestor(destinationNode, node)) Path.Remove(node); SelectedNode = destinationNode; }