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); } }
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); }