Exemplo n.º 1
0
        private void InsertTop_OnClick(object sender, RoutedEventArgs e)
        {
            if (_keyboardFocusedNCell == null) return;

            var currentRow = (int)_keyboardFocusedNCell.GetValue(Grid.RowProperty);
            var columnCount = _nTableData.Columns.Count;

            NCellContainer.RowDefinitions.Insert(currentRow, new RowDefinition() { Height = new GridLength(_nTableData.NCellDefaultHeight) });
            for (var i = currentRow; i < _nTableData.Rows.Count; i++)
            {
                _nTableData.Rows[i].ToList().ForEach(x => x.SetValue(Grid.RowProperty, (int)x.GetValue(Grid.RowProperty) + 1));
            }

            //如果当前行中有合并项,则修改合并项的根单元格的RowSpan
            var currentRowMergeItem = _nTableData.MergeCollection.Where(x => _nTableData.Rows[currentRow].Any(y => y.MergeId == x.Id));
            currentRowMergeItem.ToList().ForEach(x =>
            {
                if ((int)x.GetLeftTopNCell().GetValue(Grid.RowProperty) < currentRow)
                {
                    x.RootNCell.SetValue(Grid.RowSpanProperty, ((int)x.RootNCell.GetValue(Grid.RowSpanProperty) + 1));
                }
            });

            var newRow = new NTableRow();
            for (var i = 0; i < columnCount; i++)
            {
                var newNCell = new NCell { Background = Brushes.Thistle };
                newNCell.SetValue(Grid.RowProperty, currentRow);
                newNCell.SetValue(Grid.ColumnProperty, i);
                //将处于合并项的单元格加到合并项中
                if (_nTableData.Rows[currentRow][i].Visibility == Visibility.Collapsed || _nTableData.MergeCollection.Any(x => x.RootNCell.Equals(_nTableData.Rows[currentRow][i])))
                {
                    var nCell = _nTableData.MergeCollection.First(x => x.Id == _nTableData.Rows[currentRow][i].MergeId).GetLeftTopNCell();
                    if ((int)nCell.GetValue(Grid.RowProperty) < currentRow)
                    {
                        _nTableData.MergeCollection.First(x => x.Id == _nTableData.Rows[currentRow][i].MergeId).AppendNCell(newNCell);
                    }
                }

                NCellContainer.Children.Add(newNCell);
                _nTableData.Columns[i].Insert(currentRow, newNCell);
                newRow.Add(newNCell);
            }

            _nTableData.Rows.Insert(currentRow, newRow);
        }
Exemplo n.º 2
0
        private void Build_OnClick(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(RowInput.Text) || string.IsNullOrEmpty(ColumnInput.Text))
            {
                MessageBox.Show("输入点儿东西撒");
                return;
            };

            var rows = int.Parse(RowInput.Text);
            var columns = int.Parse(ColumnInput.Text);

            ResetNTable();

            for (int i = 0; i < rows; i++)
            {
                var rowDef = new RowDefinition() { Height = new GridLength(_nTableData.NCellDefaultHeight) };
                NCellContainer.RowDefinitions.Add(rowDef);
            }

            var columnDefaultWidth = _nTableData.NTableDefaultWidth / columns;
            _nTableData.NCellDefaultWidth = columnDefaultWidth;
            for (int i = 0; i < columns; i++)
            {
                var columnDef = new NTabelColumnDefinition(){ Width = new GridLength(columnDefaultWidth) };
                NCellContainer.ColumnDefinitions.Add(columnDef);

               var  thumb = new Thumb();
                var rd = new ResourceDictionary
                {
                    Source = new Uri("pack://application:,,,/NTable.Core;component/Themes/Common.xaml")
                };
                thumb.Style = (Style)rd["DragThumbVerStyle"];
                thumb.SetValue(Grid.ColumnProperty,i);
                thumb.HorizontalAlignment=HorizontalAlignment.Right;
                NCellContainer.Children.Add(thumb);
            }

            for (var i = 0; i < rows; i++)
            {
                var row = new NTableRow();
                for (var j = 0; j < columns; j++)
                {
                    var nCell = new NCell { Content = i + "," + j };
                    nCell.SetValue(Grid.RowProperty, i);
                    nCell.SetValue(Grid.ColumnProperty, j);
                    NCellContainer.Children.Add(nCell);
                    row.Add(nCell);
                }
                _nTableData.Rows.Add(row);
            }

            for (var i = 0; i < columns; i++)
            {
                var nCellCollection = new NTableColumn();
                for (var j = 0; j < rows; j++)
                {
                    nCellCollection.Add((NCell)NCellContainer.Children[j * columns + i]);
                }
                _nTableData.Columns.Add(nCellCollection);
            }
        }
Exemplo n.º 3
0
        private void InsertRight_OnClick(object sender, RoutedEventArgs e)
        {
            if (_keyboardFocusedNCell == null) return;

            var rowCount = _nTableData.Rows.Count;
            var currentColumn = 0;
            //如果当前操作元素是合并项
            if (_keyboardFocusedNCell.MergeId != Guid.Empty)
            {
                var rightBottomNCell = _nTableData.MergeCollection.First(x => x.Id == _keyboardFocusedNCell.MergeId).GetRightBottomNCell();
                currentColumn = (int)rightBottomNCell.GetValue(Grid.ColumnProperty);
            }
            else
            {
                currentColumn = (int)_keyboardFocusedNCell.GetValue(Grid.ColumnProperty);
            }

            NCellContainer.ColumnDefinitions.Insert(currentColumn, new ColumnDefinition() { Width = new GridLength(_nTableData.NCellDefaultWidth) });
            for (var i = currentColumn + 1; i < _nTableData.Columns.Count; i++)
            {
                _nTableData.Columns[i].ToList().ForEach(x => x.SetValue(Grid.ColumnProperty, (int)x.GetValue(Grid.ColumnProperty) + 1));
            }

            //如果当前列中有合并项,并且当前列不是合并项的最后一列,则修改合并项的根单元格的ColumnSpan
            var currentRowMergeItem = _nTableData.MergeCollection.Where(x => _nTableData.Columns[currentColumn].Any(y => y.MergeId == x.Id));
            currentRowMergeItem.ToList().ForEach(x =>
            {
                if ((int)x.GetRightBottomNCell().GetValue(Grid.ColumnProperty) > currentColumn)
                {
                    x.RootNCell.SetValue(Grid.ColumnSpanProperty, ((int)x.RootNCell.GetValue(Grid.ColumnSpanProperty) + 1));
                }
            });

            NCellContainer.Width += _nTableData.NCellDefaultWidth;
            var newColumn = new NTableColumn();
            for (var i = 0; i < rowCount; i++)
            {
                var newNCell = new NCell { Background = Brushes.DarkGray };
                newNCell.SetValue(Grid.RowProperty, i);
                newNCell.SetValue(Grid.ColumnProperty, currentColumn + 1);

                //将处于合并项的单元格加到合并项中
                if (_nTableData.Columns[currentColumn][i].Visibility == Visibility.Collapsed || _nTableData.MergeCollection.Any(x => x.RootNCell.Equals(_nTableData.Columns[currentColumn][i])))
                {
                    var nCell = _nTableData.MergeCollection.First(x => x.Id == _nTableData.Columns[currentColumn][i].MergeId).GetRightBottomNCell();
                    if ((int)nCell.GetValue(Grid.ColumnProperty) > currentColumn)
                    {
                        _nTableData.MergeCollection.First(x => x.Id == _nTableData.Columns[currentColumn][i].MergeId).AppendNCell(newNCell);
                    }
                }
                NCellContainer.Children.Add(newNCell);
                _nTableData.Rows[i].Insert(currentColumn + 1, newNCell);
                newColumn.Add(newNCell);
            }

            _nTableData.Columns.Insert(currentColumn + 1, newColumn);
        }