// create group row to show column footer cells // NOTE: // using a group row because that is read-only and provides // automatic aggregates when the column's GroupAggregate // property is set. void AddColumnFooter(C1.WPF.FlexGrid.C1FlexGrid flex) { flex.ColumnFooters.Columns.Add(new Column()); var gr = new C1.WPF.FlexGrid.GroupRow(); // customize appearance of the new row gr.FontWeight = FontWeights.Bold; gr.Background = new SolidColorBrush(Color.FromArgb(0xff, 0x80, 0x00, 0x00)); gr.Foreground = new SolidColorBrush(Colors.White); // add the row to the ColumnFooters GridPanel flex.ColumnFooters.Rows.Add(gr); gr[0] = "Totals"; }
// checks whether there is a row after this one with a given level static bool CheckNodeAfter(GroupRow row, int level) { var rows = row.Grid.Rows; for (int i = row.Index + 1; i < rows.Count; i++) { var r = rows[i] as GroupRow; if (r.Level < level) { return false; } if (r.Level == level) { return true; } } return false; }
// build a song to represent a group // the GetChildDataItems method returns all songs that belong // to this node, and the LINQ statement below calculates the total // size, length, and average rating for the album/artist. Song BuildGroupDataItem(GroupRow gr) { var gs = gr.GetDataItems().OfType<Song>(); return new Song() { Name = gr.Group.Name.ToString(), Size = (long)gs.Sum(s => s.Size), Duration = (long)gs.Sum(s => s.Duration), Rating = (int)(gs.Average(s => s.Rating) + 0.5) }; }
private static FrameworkElement GetGlyphTree(GroupRow gr, bool collapsed, Brush brush) { bool groupRowAboveData = gr.Grid.GroupRowPosition == GroupRowPosition.AboveData; Border border = new Border(); border.Background = _brTransparent; border.MinWidth = gr.Grid.TreeIndent; if (gr.HasChildren) { Polygon polygon; if (collapsed) { double[] values = {0, 0, 0, 8, 5, 4}; polygon = Util.Util.CreatePolygon(brush, values); } else if (groupRowAboveData) { double[] values = {0, 6, 6, 6, 6, 0}; polygon = Util.Util.CreatePolygon(brush, values); } else { double[] values = {0, 0, 6, 0, 6, 6}; polygon = Util.Util.CreatePolygon(brush, values); } border.Child = polygon; border.MouseEnter += (s, e) => polygon.Fill = _brBlue; border.MouseLeave += (s, e) => polygon.Fill = brush; border.MouseLeftButtonDown += (s, e) => { e.Handled = true; int index = gr.Index; if (!gr.Grid.FinishEditing()) { return; } Util.Util.SetFocus(gr.Grid); if (index <= -1 || gr.Grid.Rows.Count <= index) { return; } gr = gr.Grid.Rows[index] as GroupRow; if (Kbd.IsControlPressed()) { if (gr != null) { gr.Grid.CollapseGroupsToLevel((gr.IsCollapsed ? gr.Level + 1 : gr.Level)); } return; } if (gr != null) { gr.IsCollapsed = !gr.IsCollapsed; } }; } return border; }
public NodeCell(Row row) : base(row) { // create collapsed/expanded images if (_bmpExpanded == null) { _bmpExpanded = ImageCell.GetImageSource("Expanded.png"); _bmpCollapsed = ImageCell.GetImageSource("Collapsed.png"); } // store reference to row _gr = row as GroupRow; // initialize collapsed/expanded image _nodeImage = new Image(); _nodeImage.Source = _gr.IsCollapsed ? _bmpCollapsed : _bmpExpanded; _nodeImage.Width = _nodeImage.Height = 9; _nodeImage.VerticalAlignment = VerticalAlignment.Center; _nodeImage.Stretch = Stretch.None; _nodeImage.MouseLeftButtonDown += img_MouseLeftButtonDown; _nodeImage.MouseEnter += img_MouseEnter; _nodeImage.MouseLeave += img_MouseLeave; _nodeImage.Opacity = ALPHA; Children.Insert(0, _nodeImage); // make text bold TextBlock.FontWeight = FontWeights.Bold; }
void AddPersonToGrid(Person p, C1FlexGrid flex, int level) { // create a row for this person Row row; if (p.Children.Count > 0 || true) { var gr = new GroupRow(); gr.Level = level; row = gr; } else { row = new Row(); } row.DataItem = p; // add this person to the grid flex.Rows.Add(row); // and add any children foreach (var child in p.Children) { AddPersonToGrid(child, flex, level + 1); } }
private int InsertGroupRows(CollectionViewGroup group, int level, int index, Dictionary<object, GroupRow> groupRows) { GroupRow groupRow; INotifyCollectionChanged items = group.Items; if (items != null) { items.CollectionChanged -= ncc_CollectionChanged; items.CollectionChanged += ncc_CollectionChanged; } if (!groupRows.TryGetValue(groupRows, out groupRow)) { groupRow = new GroupRow(); } groupRow.Level = level; groupRow.Group = group; int i = index; if (!group.IsBottomLevel) { foreach (CollectionViewGroup item in group.Items) { i = InsertGroupRows(item, level + 1, i, groupRows); } } else { i += group.ItemCount; } bool collapsed; if (GroupRowPosition != GroupRowPosition.AboveData) { collapsed = i < Rows.Count && Rows[i].GetFlag(RowColFlags.ParentCollapsed); Rows.Insert(i, groupRow); } else { collapsed = (index < Rows.Count && Rows[index].GetFlag(RowColFlags.ParentCollapsed)); Rows.Insert(index, groupRow); } groupRow.SetFlag(RowColFlags.Collapsed, collapsed); return i + 1; }
private void AddHierarchicalRow(object item, IEnumerable children, int level) { GroupRow groupRow = new GroupRow(); groupRow.Level = level; groupRow.IsReadOnly = false; groupRow.DataItem = item; if (GroupRowPosition == GroupRowPosition.AboveData) { Rows.Add(groupRow); } if (children != null) { INotifyCollectionChanged ncc = children as INotifyCollectionChanged; if (ncc != null) { _nccChildren.Add(ncc); ncc.CollectionChanged += nccChildren_CollectionChanged; } foreach (object child in children) { PropertyInfo property = child.GetType().IsAssignableFrom(ItemType) ? _piChildItems : child.GetType().GetProperty(ChildItemsPath); IList children2; if (property != null) { children2 = property.GetValue(child, null) as IList; } else { children2 = null; } AddHierarchicalRow(child, children2, level + 1); } } if (GroupRowPosition == GroupRowPosition.BelowData) { Rows.Add(groupRow); } }
// creates the element that represents a tree node FrameworkElement CreateTreeNode(C1FlexGrid flex, GroupRow row) { var g = new Grid(); for (int i = 0; i <= row.Level; i++) { var cd = new ColumnDefinition(); cd.Width = new GridLength(flex.TreeIndent); g.ColumnDefinitions.Add(cd); } // icon connectors (left and up) if (row.Level > 0) { var rc = CreateConnector(flex.TreeIndent, _connThickness); rc.HorizontalAlignment = HorizontalAlignment.Left; rc.VerticalAlignment = VerticalAlignment.Center; rc.Margin = new Thickness(flex.TreeIndent / 2, 0, 0, 0); rc.SetValue(Grid.ColumnProperty, row.Level - 1); rc.SetValue(Grid.ColumnSpanProperty, 2); g.Children.Add(rc); rc = CreateConnector(_connThickness, row.ActualHeight / 2); rc.HorizontalAlignment = HorizontalAlignment.Center; rc.VerticalAlignment = VerticalAlignment.Top; rc.SetValue(Grid.ColumnProperty, row.Level - 1); g.Children.Add(rc); } // icon connector (down) if (row.HasChildren && !row.IsCollapsed) { var rc = CreateConnector(_connThickness, row.ActualHeight / 2); rc.HorizontalAlignment = HorizontalAlignment.Center; rc.VerticalAlignment = VerticalAlignment.Bottom; rc.SetValue(Grid.ColumnProperty, row.Level); g.Children.Add(rc); } // inter-node connectors for (int i = row.Level; i > 0; i--) { if (CheckNodeAfter(row, i)) { var rc = CreateConnector(_connThickness, row.ActualHeight); rc.HorizontalAlignment = HorizontalAlignment.Center; rc.VerticalAlignment = VerticalAlignment.Top; rc.SetValue(Grid.ColumnProperty, i - 1); g.Children.Add(rc); } } // icon var nodeIcon = new Image(); nodeIcon.Source = row.HasChildren == false ? _bmpNoChildren : row.IsCollapsed ? _bmpCollapsed : _bmpExpanded; nodeIcon.Stretch = Stretch.None; nodeIcon.SetValue(Grid.ColumnProperty, row.Level + 1); g.Children.Add(nodeIcon); if (row.HasChildren) { nodeIcon.MouseLeftButtonDown += (s, e) => { row.IsCollapsed = !row.IsCollapsed; }; } // done g.Margin = new Thickness(0, 0, 4, 0); return g; }