Silverlight之Grid后台添加或删除新行
前台:
<Grid x:Name="MemberReport"> </Grid>
后台:
给Grid添加新行
RowDefinition rd = new RowDefinition(); GridLength gl = new GridLength(25); rd.Height = gl; MemberReport.RowDefinitions.Add(rd); for (int i = 4; i <= MemberReport.RowDefinitions.Count; i++) { for (int j = 0; j < MemberReport.ColumnDefinitions.Count; j++) { Border border = new Border(); border.Name = "Border_" + i;
border.BorderThickness = new Thickness(0, 0, 1, 1); border.BorderBrush = new SolidColorBrush(color);
border.SetValue(Grid.RowProperty, i); border.SetValue(Grid.ColumnProperty, j); TextBlock tb = new TextBlock(); tb.HorizontalAlignment = HorizontalAlignment.Left; tb.Margin = new Thickness(5, 0, 0, 0); tb.VerticalAlignment = VerticalAlignment.Center; tb.TextAlignment = TextAlignment.Center; tb.Text = "预存款客人姓名:"; border.Child = tb; MemberReport.Children.Add(border); } }
删除新行:当Grid要删除行时,不仅要删除行还要删除行中的控件
private void btnMinus_Click(object sender, RoutedEventArgs e) { MemberReport.RowDefinitions.RemoveAt(count); Border border = MemberReport.FindName("Border_" + count) as Border; MemberReport.Children.Remove(border); }