protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved) { var added = visualAdded != null; var removed = visualRemoved != null; if (added != removed) { if (Orientation == Orientation.Horizontal) { if (removed) { ColumnDefinitions.RemoveAt(ColumnDefinitions.Count - 1); } else { ColumnDefinitions.Add(new ColumnDefinition()); visualAdded.SetValue(Grid.ColumnProperty, ColumnDefinitions.Count - 1); } } else { if (removed) { RowDefinitions.RemoveAt(RowDefinitions.Count - 1); } else { RowDefinitions.Add(new RowDefinition()); visualAdded.SetValue(Grid.RowProperty, RowDefinitions.Count - 1); } } } base.OnVisualChildrenChanged(visualAdded, visualRemoved); }
private void SetupColumns() { var children = Children.OfType <FrameworkElement>().ToList(); if (ColumnDefinitions.Count < children.Count) { for (int i = ColumnDefinitions.Count; i < children.Count; i++) { ColumnDefinitions.Add(new ColumnDefinition()); } } else if (ColumnDefinitions.Count > children.Count) { for (int i = ColumnDefinitions.Count - 1; i >= children.Count; i--) { ColumnDefinitions.RemoveAt(i); } } for (int i = 0; i < children.Count; i++) { bool firstRightAligned = true; var col = ColumnDefinitions[i]; var fe = children[i] as FrameworkElement; Grid.SetColumn(fe, i); if (fe.HorizontalAlignment == HorizontalAlignment.Left) { col.Width = new GridLength(1, GridUnitType.Auto); } else if (fe.HorizontalAlignment == HorizontalAlignment.Center) { col.Width = new GridLength(1, GridUnitType.Star); } else if (fe.HorizontalAlignment == HorizontalAlignment.Right) { if (firstRightAligned) { col.Width = new GridLength(1, GridUnitType.Star); } else { col.Width = new GridLength(1, GridUnitType.Auto); } firstRightAligned = false; } else if (fe.HorizontalAlignment == HorizontalAlignment.Stretch) { col.Width = new GridLength(1, GridUnitType.Star); } } }
private void updateColumnDefinitions() { while (ColumnDefinitions.Count < ColumnCount) { var definition = new ColumnDefinition(); definition.Width = new GridLength(1, GridUnitType.Star); ColumnDefinitions.Add(definition); } while (ColumnDefinitions.Count > ColumnCount) { ColumnDefinitions.RemoveAt(ColumnDefinitions.Count - 1); } }
public void OnSizeChanged() { // добавляем нехватающие колонки for (int i = ColumnDefinitions.Count; i < App.Settings.images_in_row; i++) { ColumnDefinitions.Add(new ColumnDefinition()); } // убираем лишние колонки for (int i = ColumnDefinitions.Count - 1; i >= App.Settings.images_in_row; i--) { ColumnDefinitions.RemoveAt(i); } // перещитываем положения List <double> heights = new List <double>(); for (int i = 0; i < App.Settings.images_in_row; i++) { heights.Add(0); } foreach (ItemData item in ItemsData) { KeyValuePair <int, double> columnHeight = GetMinColumn(heights); // перещитываем новую высоту учитывая новую ширину item.CalcHeight(); // заполняем новыми данными heights[columnHeight.Key] += item.Height; item.Column = columnHeight.Key; item.MarginTop = columnHeight.Value; // если объект был на экране перерисовываем if (item.isVisible) { item.ReShow(); } } // проверяем корректность CheckVisible(VerticalOffset); }
private void OnSectionsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (_viewModel == null) { return; } // remove extra children while (Children.Count > _viewModel.Sections.Count) { var child = (OnScreenKeyboardSection)Children[Children.Count - 1]; child.DataContext = null; // remote the extra child and its column definition Children.Remove(child); ColumnDefinitions[ColumnDefinitions.Count - 1].ClearValue(ColumnDefinition.WidthProperty); ColumnDefinitions.RemoveAt(ColumnDefinitions.Count - 1); } // add enough section controls to match the number of section view models we have while (Children.Count < _viewModel.Sections.Count) { Children.Add(new OnScreenKeyboardSection(this)); ColumnDefinitions.Add(new ColumnDefinition()); } // now that we have an exact number of columns, // matching the exact number of controls, // update all the data contexts foreach (var section in _viewModel.Sections) { var index = _viewModel.Sections.IndexOf(section); var keyboardSection = ((OnScreenKeyboardSection)Children[index]); keyboardSection.DataContext = section; SetColumn(keyboardSection, index); ColumnDefinitions[index].SetBinding(ColumnDefinition.WidthProperty, new Binding("SectionWidth") { Source = section }); } }
public void Undock(IDockLayout child) { int index = m_children.IndexOf(child); if (Orientation == Orientation.Horizontal) { ColumnDefinitions.RemoveAt(index * 2); Children.RemoveAt(index * 2); if (m_children.Count > 1) { ColumnDefinitions.RemoveAt(index * 2 + (index == 0 ? 0 : -1)); Children.RemoveAt(index * 2 + (index == 0 ? 0 : -1)); for (int i = 0; i < Children.Count; i++) { Grid.SetColumn(Children[i], i); } } } else { RowDefinitions.RemoveAt(index * 2); Children.RemoveAt(index * 2); if (m_children.Count > 1) { RowDefinitions.RemoveAt(index * 2 + (index == 0 ? 0 : -1)); Children.RemoveAt(index * 2 + (index == 0 ? 0 : -1)); for (int i = 0; i < Children.Count; i++) { Grid.SetRow(Children[i], i); } } } m_children.RemoveAt(index); Root.CheckConsistency(); }
private void ResizeGrid() { if (_keys == null) { Reset(); return; } // Make sure there's the right number of rows var rowCount = _keys.Max(x => x.GridRow) + 1; for (var rowsToAdd = rowCount - RowDefinitions.Count; rowsToAdd > 0; rowsToAdd--) { // Add the extra Row RowDefinitions.Add(new RowDefinition()); } for (var rowsToRemove = RowDefinitions.Count - rowCount; rowsToRemove > 0; rowsToRemove--) { // Remove the extra Row RowDefinitions.RemoveAt(0); } // Make sure there's the right number of cols var colCount = _keys.Max(x => x.GridColumn) + 1; for (var colsToAdd = colCount - ColumnDefinitions.Count; colsToAdd > 0; colsToAdd--) { // Add the extra Column ColumnDefinitions.Add(new ColumnDefinition()); } for (var colsToRemove = ColumnDefinitions.Count - colCount; colsToRemove > 0; colsToRemove--) { // Remove the extra Column ColumnDefinitions.RemoveAt(0); } }
/// <summary> /// Will merge itself with given grid. Basicly it takes child grid, removes that grid, and all /// children of removed grid will be added to this grid. The child grid must have same orientation /// or maximum of 1 child. /// </summary> /// <param name="grid">Grid to merge with</param> internal void MergeWith(GridLayout grid) { // get the index of the grid to merge with (it is child of this grid) int index = m_children.IndexOf(grid); double totalValue = 0; int indexOffset = 0; if (Orientation == Orientation.Horizontal) { // get the column definition of child grid ColumnDefinition oldColumnDef = ColumnDefinitions[index * 2]; // count the sum of columns widths of childs children foreach (IDockLayout newLayout in grid.Layouts) { FrameworkElement element = (FrameworkElement)newLayout; int columnIndex = Grid.GetColumn(element); ColumnDefinition layoutColumnDef = grid.ColumnDefinitions[columnIndex]; totalValue += layoutColumnDef.Width.Value; } // remove the child from our list, from view children adn from column definitions m_children.RemoveAt(index); Children.RemoveAt(index * 2); ColumnDefinitions.RemoveAt(index * 2); // move all childs children to this grid and create column definitions for each of them foreach (IDockLayout newLayout in grid.Layouts) { FrameworkElement element = (FrameworkElement)newLayout; int columnIndex = Grid.GetColumn(element); ColumnDefinition layoutColumnDef = grid.ColumnDefinitions[columnIndex]; double newValue = oldColumnDef.Width.Value * layoutColumnDef.Width.Value / totalValue; ColumnDefinition newColumnDef = NewColumnDefinition(new GridLength(newValue, GridUnitType.Star), m_minGridSize.Width); grid.Children.Remove(element); ColumnDefinitions.Insert((index + indexOffset) * 2, newColumnDef); Children.Insert((index + indexOffset) * 2, element); m_children.Insert(index + indexOffset, newLayout); if (indexOffset < grid.Layouts.Count - 1) { // if we move more than one child, then we need to add new splitters too GridSplitter splitter = NewGridSplitter(Orientation.Horizontal); ColumnDefinitions.Insert((index + indexOffset) * 2 + 1, NewColumnDefinition(new GridLength(1, GridUnitType.Auto), 0)); Children.Insert((index + indexOffset) * 2 + 1, splitter); } indexOffset++; } // set the association of column indexes for (int i = 0; i < Children.Count; i++) { Grid.SetColumn(Children[i], i); } } else { // get the row definition of child grid RowDefinition oldRowDef = RowDefinitions[index * 2]; // count the sum of row heights of childs children foreach (IDockLayout newLayout in grid.Layouts) { FrameworkElement element = (FrameworkElement)newLayout; int RowIndex = Grid.GetRow(element); RowDefinition layoutRowDef = grid.RowDefinitions[RowIndex]; totalValue += layoutRowDef.Height.Value; } // remove the child from our list, from view children adn from row definitions m_children.RemoveAt(index); Children.RemoveAt(index * 2); RowDefinitions.RemoveAt(index * 2); // move all childs children to this grid and create row definitions for each of them foreach (IDockLayout newLayout in grid.Layouts) { FrameworkElement element = (FrameworkElement)newLayout; int RowIndex = Grid.GetRow(element); RowDefinition layoutRowDef = grid.RowDefinitions[RowIndex]; double newValue = oldRowDef.Height.Value * layoutRowDef.Height.Value / totalValue; RowDefinition newRowDef = NewRowDefinition(new GridLength(newValue, GridUnitType.Star), m_minGridSize.Height); grid.Children.Remove(element); RowDefinitions.Insert((index + indexOffset) * 2, newRowDef); Children.Insert((index + indexOffset) * 2, element); m_children.Insert(index + indexOffset, newLayout); if (indexOffset < grid.Layouts.Count - 1) { // if we move more than one child, then we need to add new splitters too GridSplitter splitter = NewGridSplitter(Orientation.Vertical); RowDefinitions.Insert((index + indexOffset) * 2 + 1, NewRowDefinition(new GridLength(1, GridUnitType.Auto), 0)); Children.Insert((index + indexOffset) * 2 + 1, splitter); } indexOffset++; } // set the association of row indexes for (int i = 0; i < Children.Count; i++) { Grid.SetRow(Children[i], i); } } }
/// <summary> /// Delete compoenent from the rung /// </summary> /// <param name="component">Component to be deleted</param> public RungUI Remove(ComponentUIBase component) { ComponentGridPosition _component = _Components.Where(x => x.Component == component).First(); if (_LogicalRung.Components.Where(x => x.LeftLide == component.LogicComponent.LeftLide && x.RightLide == component.LogicComponent.RightLide).Count() > 1) { foreach (ComponentGridPosition item in GetAllBetween(component.LogicComponent.LeftLide, component.LogicComponent.RightLide).Where(x => x.Row > _component.Row)) { item.SetPossition(item.Row - 1, item.Column); } } else { IEnumerable <ComponentGridPosition> RR = _Components.Where(x => x.Component.LogicComponent.RightLide == component.LogicComponent.RightLide && x != _component); if (RR.Count() > 0) { IEnumerable <ComponentGridPosition> column_components = _Components.Where(x => x.Column == _component.Column); if (column_components.Count() == 1) { foreach (ComponentGridPosition item in _Components.Where(x => x.Column > _component.Column)) { item.SetPossition(item.Row, item.Column - 1); } } else { foreach (ComponentGridPosition item in GetAllBetween(component.LogicComponent.LeftLide, component.LogicComponent.RightLide).Where(x => x.Row > _component.Row)) { item.SetPossition(item.Row - 1, item.Column); } } } else { foreach (ComponentGridPosition item in _Components.Where(x => x.Component.LogicComponent.LeftLide == component.LogicComponent.RightLide)) { item.SetPossition(item.Row, item.Column - 1); } } } _Components.Remove(_component); _LogicalRung.Remove(component.LogicComponent); Children.Remove(component); var row = RowDefinitions.Where(x => _Components.Where(y => (y.Row == RowDefinitions.IndexOf(x))).Count() == 0); if (row.Count() != 0) { int row_index = RowDefinitions.IndexOf(row.First()); foreach (ComponentGridPosition item in _Components.Where(x => x.Row > row_index)) { item.SetPossition(item.Row - 1, item.Column); } RowDefinitions.RemoveAt(row_index); } var column = ColumnDefinitions.Where(x => _Components.Where(y => (y.Column == ColumnDefinitions.IndexOf(x))).Count() == 0); if (column.Count() != 0) { int column_index = ColumnDefinitions.IndexOf(column.First()); if (_component.Column >= ColumnDefinitions.Count - _OutputBlockLegth) { _OutputBlockLegth--; } foreach (ComponentGridPosition item in _Components.Where(x => x.Column > column_index)) { item.SetPossition(item.Row, item.Column - 1); } ColumnDefinitions.RemoveAt(column_index); } return(this); }