/// <summary> /// Append an item to it's parent and update the layout /// </summary> /// <param name="grid"></param> /// <param name="item"></param> public void AppendItemToGrid(Grid grid, TreeGridItem item) { RowDefinition row = new RowDefinition(); grid.RowDefinitions.Add(row); grid.Children.Add(item); Grid.SetColumn(item, 0); Grid.SetRow(item, grid.RowDefinitions.Count - 1); item.HorizontalAlignment = HorizontalAlignment.Left; item.VerticalAlignment = VerticalAlignment.Top; TreeGridItem parent = GetDisplay(item.Model.Parent); if (parent != null) { Brush stroke = new SolidColorBrush(Color.FromArgb((byte)100, (byte)0, (byte)0, (byte)0.5)); TreeGridLine line = new TreeGridLine() { Line1 = new Line() { Stroke = stroke, StrokeThickness = 2 }, Line2 = new Line() { Stroke = stroke, StrokeThickness = 2 }, Line3 = new Line() { Stroke = stroke, StrokeThickness = 2 }, Start = parent, End = item }; UnderlayCanvas.Children.Add(line.Line1); UnderlayCanvas.Children.Add(line.Line2); UnderlayCanvas.Children.Add(line.Line3); _lines.Add(line); } UpdateDimensions(item, true); row.Height = new GridLength(item.Height); }
private void RemoveFromChildCanvases(BetTreeNodeModel node, TreeGridItem parentDisplay) { TreeGridItem nodeDisplay = _items.Find(x => x.DataContext == node); foreach (BetTreeNodeModel child in node.Children) RemoveFromChildCanvases(child, nodeDisplay); TreeGridStart start = GetGrid(parentDisplay, null); if (start != null) RemoveItem(start.MainGrid, nodeDisplay); }
private void UpdateGridDimensions(BetTreeNodeModel node, TreeGridItem itemDisplay, TreeGridItem parentDisplay) { TreeGridStart grid = GetGrid(parentDisplay, StartGrid); double height = 0, width = 0; for (int i = 0; i < grid.MainGrid.Children.Count; i++) { TreeGridItem child = grid.MainGrid.Children[i] as TreeGridItem; if (child.Visibility == Visibility.Visible) { if (child.Width > width) width = child.Width; height += child.Height; } if (child == itemDisplay) { grid.MainGrid.RowDefinitions[i].Height = new GridLength(child.Height); } } grid.MainGrid.ColumnDefinitions[0].Width = new GridLength(width); grid.Width = width; grid.Height = height; }
protected bool RemoveItem(Grid parent, TreeGridItem child) { bool found = false; for (int i = 0; i < parent.Children.Count; i++) { if (parent.Children[i] == child) { parent.Children.RemoveAt(i); parent.RowDefinitions.RemoveAt(i); found = true; if (i < parent.Children.Count) Grid.SetRow(parent.Children[i], i); } else if (found) { Grid.SetRow(parent.Children[i], i); } } List<TreeGridLine> lines = _lines.Where(x => x.Start == child || x.End == child).ToList(); foreach (TreeGridLine line in lines) { UnderlayCanvas.Children.Remove(line.Line1); UnderlayCanvas.Children.Remove(line.Line2); UnderlayCanvas.Children.Remove(line.Line3); _lines.Remove(line); } return found; }
protected void RootItemLayoutUpdated(TreeGridItem item) { BetTreeNodeModel node = item.DataContext as BetTreeNodeModel; TreeGridItem parentDisplay = _items.Find(x => x.DataContext == node.Parent); UpdateGridDimensions(node, item, parentDisplay); if (parentDisplay != null) { UpdateDimensions(parentDisplay, true); } UpdateCanvas(item.Model.Snapshot.Round); }
protected double GetGridStartTop(TreeGridItem item) { Canvas parent = _canvases[item.Model.Snapshot.Round]; Point relativeLocation = item.TranslatePoint(new Point(0, 0), parent); return relativeLocation.Y; }
/// <summary> /// /// </summary> /// <param name="node"></param> /// <param name="parentDisplay"></param> /// <returns></returns> protected TreeGridStart GetNewGridStart(BetTreeNodeModel node, TreeGridItem parentDisplay) { TreeGridStart start = new TreeGridStart(); start.LinkedItem = parentDisplay; start.LayoutUpdated += this.RefreshCanvas; Canvas canvas = _canvases[node.Snapshot.Round]; canvas.Children.Add(start); _grids[parentDisplay] = start; return start; }
protected TreeGridStart GetGrid(TreeGridItem item, TreeGridStart defaultGrid) { if (item != null && _grids.ContainsKey(item)) return _grids[item]; else return defaultGrid; }
/// <summary> /// Adds a node to the child grid of its parent /// </summary> /// <param name="node">The node to be added</param> /// <param name="parentDisplay">The parent grid to which the node is added</param> protected void AddStandardNode(BetTreeNodeModel node, TreeGridItem parentDisplay) { Grid parentGrid = parentDisplay.Children; TreeGridItem item = GetNewItem(node); AppendItemToGrid(parentGrid, item); }
/// <summary> /// Adds a node to the root of a round section /// </summary> /// <param name="node">The node to be added to the root</param> /// <param name="parentDisplay">The node parent from the previous round, or null of this is the first round</param> protected void AddRootNode(BetTreeNodeModel node, TreeGridItem parentDisplay) { TreeGridStart start = (parentDisplay == null) ? StartGrid : GetGrid(parentDisplay, null); if (start == null) { start = GetNewGridStart(node, parentDisplay); } TreeGridItem item = GetNewRootItem(node); AppendItemToGrid(start.MainGrid, item); UpdateGridDimensions(node, item, parentDisplay); UpdateCanvas(node.Snapshot.Round); }
/// <summary> /// Update the dimensions of a single grid item /// </summary> /// <param name="item">The item to be updated</param> /// <param name="expanded">Whether or not the item is expanded</param> public void UpdateDimensions(TreeGridItem item, bool expanded) { double childGridWidth = 0, childGridHeight = 0; for (int i = 0; expanded && i < item.Model.Children.Count; i++) { TreeGridItem child = GetDisplay(item.Model.Children[i]); if (child != null) { if (child.Visibility != Visibility.Visible) continue; if (child.Width > childGridWidth && child.Model.Snapshot.Round == item.Model.Snapshot.Round) childGridWidth = child.Width; if (i < item.Children.RowDefinitions.Count) item.Children.RowDefinitions[i].Height = new GridLength(child.Height); childGridHeight += child.Height; } } item.FixLayout(childGridWidth, childGridHeight); }
/// <summary> /// Get a new round root item and add the appropriate event handlers /// </summary> /// <param name="node">The node model from which the visual component should be derived</param> /// <returns>Visual representation of a node</returns> public TreeGridItem GetNewRootItem(BetTreeNodeModel node) { TreeGridItem item = new TreeGridItem(); item.DoFixLayout += this.UpdateDimensions; item.ItemLayoutUpdated += this.RootItemLayoutUpdated; _items.Add(item); item.LayoutUpdated += this.RefreshCanvas; item.DataContext = node; node.ExpandCollapse += DoExpandCollapse; return item; }