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