動態新增Grid欄列
使用grid.RowDefinitons.Add(new RowDefinition()) 及 grid.ColumnDefinitions.Add(new ColumnDefinition()). 如下代碼
for (int i = 0; i < 10; i++) { grid.RowDefinitions.Add(new RowDefinition()); } for (int i = 0; i < 20; i++) { grid.ColumnDefinitions.Add(new ColumnDefinition()); }
動態加入控制項
Grid.SetColumn()及Grid.SetRow()設定欄列
for (int i = 0; i < 10; i++) { for (int j = 0; j < 20; j++) { Label l = new Label(); l.BorderBrush = Brushes.Gray; l.BorderThickness = new Thickness(0.2,0.2,0.2,0.2); l.Content = string.Format("({0:d2},{1:d2})", j+1, i+1); l.VerticalContentAlignment = VerticalAlignment.Center; l.HorizontalContentAlignment = HorizontalAlignment.Center; Grid.SetColumn(l, j); Grid.SetRow(l, i); grid.Children.Add(l); //l.Background = Brushes.Red; //l.Background=new SolidColorBrush(Color.FromArgb(255,100,100,255)); } }
todo