Grid自动添加行
#region 自动添加行 private void InitRows(int rowCount, Grid g) { while (rowCount-- > 0) { RowDefinition rd = new RowDefinition(); rd.Height = new GridLength(60); g.RowDefinitions.Add(rd); } } private void InitColumns(int colCount, Grid g) { while (colCount-- > 0) { ColumnDefinition rd = new ColumnDefinition(); rd.Width = new GridLength(); g.ColumnDefinitions.Add(rd); } } string stringF4 = "{0:F4}"; //列数为4列 private void Init(Grid g, int rowcount) { InitRows(rowcount, g); InitColumns(4, g); #region for (int r = 1; r <= rowcount; r++) { if (r < rowcount) { #region for (int c = 0; c <= 4; c++) { Fi_VoucherChild child = new Fi_VoucherChild(); if (c == 0) { TextBoxLink text = new TextBoxLink(); text.Name = "text" + r + c; text.FontSize = 14; text.Height = g.Height; text.LinkText = "摘要"; text.LinkClick += text_LinkClick; Binding txtbinding = new Binding("Abstract"); txtbinding.StringFormat = stringF4; text.SetBinding(TextBox.TextProperty, txtbinding); g.Children.Add(text); Grid.SetColumn(text, c); Grid.SetRow(text, r); } if (c == 1) { ComboBoxLink text = new ComboBoxLink(); text.Height = g.Height; text.FontSize = 14; text.Name = "text" + r + c; text.LinkText = "科目"; text.LinkClick += textCol_LinkClick; text.ComboBoxLinkGotFocus += text_GotFocus; g.Children.Add(text); Grid.SetColumn(text, c); Grid.SetRow(text, r); } if (c == 2) { TextBox text = new TextBox(); text.Height = g.Height; text.FontSize = 28; text.Name = "text" + r + c; text.SpellCheck.IsEnabled = true; text.TextAlignment = System.Windows.TextAlignment.Right; text.LostFocus += text_LostFocus; g.Children.Add(text); Grid.SetColumn(text, c); Grid.SetRow(text, r); } if (c == 3) { TextBox text = new TextBox(); text.FontSize = 28; text.Height = g.Height; text.TextAlignment = System.Windows.TextAlignment.Right; text.LostFocus += textT_LostFocus; text.Name = "text" + r + c; g.Children.Add(text); Grid.SetColumn(text, c); Grid.SetRow(text, r); } } #endregion } else { for (int c = 0; c < 4; c++) { if (c == 0) { TextBlock tbkSum = new TextBlock(); tbkSum.Name = "tbkSum" + r + c; tbkSum.Text = "合计:"; tbkSum.FontSize = 24; g.Children.Add(tbkSum); Grid.SetRow(tbkSum, r); Grid.SetColumnSpan(tbkSum, c + 2); } if (c != 0 && c != 1) { TextBlock tbkSum = new TextBlock(); tbkSum.Name = "tbkSum" + r + c; tbkSum.FontSize = 24; tbkSum.TextAlignment = System.Windows.TextAlignment.Right; g.Children.Add(tbkSum); Grid.SetColumn(tbkSum, c); Grid.SetRow(tbkSum, r); } } } } DataGrid dg = new DataGrid(); this.UpdateLayout(); #endregion } private void AddGrid(Grid g, int rowcount) { int rows = g.RowDefinitions.Count; InitRows(rowcount, g); InitColumns(4, g); #region for (int r = 1; r <= rowcount; r++) { #region for (int c = 0; c <= 4; c++) { Fi_VoucherChild child = new Fi_VoucherChild(); if (c == 0) { TextBoxLink text = new TextBoxLink(); text.Name = "text" + r + c; text.FontSize = 14; text.Height = g.Height; text.LinkText = "摘要"; text.LinkClick += text_LinkClick; Binding txtbinding = new Binding("Abstract"); txtbinding.StringFormat = stringF4; text.SetBinding(TextBox.TextProperty, txtbinding); g.Children.Add(text); Grid.SetColumn(text, c); Grid.SetRow(text, rows-1); } if (c == 1) { ComboBoxLink text = new ComboBoxLink(); text.Height = g.Height; text.FontSize = 14; text.Name = "text" + r + c; text.LinkText = "科目"; text.LinkClick += textCol_LinkClick; text.ComboBoxLinkGotFocus += text_GotFocus; g.Children.Add(text); Grid.SetColumn(text, c); Grid.SetRow(text, rows-1); } if (c == 2) { TextBox text = new TextBox(); text.Height = g.Height; text.FontSize = 28; text.Name = "text" + r + c; text.SpellCheck.IsEnabled = true; text.TextAlignment = System.Windows.TextAlignment.Right; text.LostFocus += text_LostFocus; g.Children.Add(text); Grid.SetColumn(text, c); Grid.SetRow(text, rows-1); } if (c == 3) { TextBox text = new TextBox(); text.FontSize = 28; text.Height = g.Height; text.TextAlignment = System.Windows.TextAlignment.Right; text.LostFocus += textT_LostFocus; text.Name = "text" + r + c; g.Children.Add(text); Grid.SetColumn(text, c); Grid.SetRow(text, rows-1); } } #endregion } DataGrid dg = new DataGrid(); this.UpdateLayout(); #endregion } #endregion