///
/// 动态添加行
///
///
///
private void addRow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
bool canAdd = true;
if (canAdd)
{
//添加字段名称
TextBlock textBlock1 = new TextBlock();
textBlock1.Text = "名 称:";
textBlock1.Margin = new Thickness(0, 0, 13, 0);
textBlock1.Height = 25;
textBlock1.HorizontalAlignment = HorizontalAlignment.Right;
textBlock1.VerticalAlignment = VerticalAlignment.Bottom;
textBlock1.TextWrapping = TextWrapping.Wrap;
Grid.SetColumn(textBlock1, 0);
Grid.SetRow(textBlock1, row);
Grid.SetColumnSpan(textBlock1, 1);
FieldGrid.Children.Add(textBlock1);
//添加字段名称文本框
TextBox textBox1 = new TextBox();
textBox1.Name = "txtField" + row;
textBox1.Margin = new Thickness(2, 4, 2, 4);
textBox1.Height = 22;
Grid.SetColumn(textBox1, 1);
Grid.SetRow(textBox1, row);
FieldGrid.Children.Add(textBox1);
//添加字段类型
TextBlock textBlock2 = new TextBlock();
textBlock2.Text = "类 型:";
textBlock2.Margin = new Thickness(0, 0, 13, 0);
textBlock2.Height = 25;
textBlock2.HorizontalAlignment = HorizontalAlignment.Right;
textBlock2.VerticalAlignment = VerticalAlignment.Bottom;
textBlock2.TextWrapping = TextWrapping.Wrap;
Grid.SetColumn(textBlock2, 2);
Grid.SetRow(textBlock2, row);
Grid.SetColumnSpan(textBlock2, 1);
FieldGrid.Children.Add(textBlock2);
//添加字段类型文本框
TextBox textBox2 = new TextBox();
textBox2.Name = "txtFieldType" + row;
textBox2.Margin = new Thickness(2, 4, 2, 4);
textBox2.Height = 22;
Grid.SetColumn(textBox2, 3);
Grid.SetRow(textBox2, row);
FieldGrid.Children.Add(textBox2);
//添加序列号
TextBlock textBlock3 = new TextBlock();
textBlock3.Text = "序 列:";
textBlock3.Margin = new Thickness(0, 0, 13, 0);
textBlock3.Height = 25;
textBlock3.HorizontalAlignment = HorizontalAlignment.Right;
textBlock3.VerticalAlignment = VerticalAlignment.Bottom;
textBlock3.TextWrapping = TextWrapping.Wrap;
Grid.SetColumn(textBlock3, 4);
Grid.SetRow(textBlock3, row);
Grid.SetColumnSpan(textBlock3, 1);
FieldGrid.Children.Add(textBlock3);
//添加字段类型文本框
TextBox textBox3 = new TextBox();
textBox3.Name = "txtSequence" + row;
textBox3.Margin = new Thickness(2, 4, 2, 4);
textBox3.Height = 22;
Grid.SetColumn(textBox3, 5);
Grid.SetRow(textBox3, row);
FieldGrid.Children.Add(textBox3);
row++;
}
else if (row > 100)
{
canAdd = false;
}
}
附: silverlight动态添加下拉列表
ComboBox cboxType = new ComboBox();
cboxType.Name = "cboxType"+row;
cboxType.Width = 100;
cboxType.Margin = new Thickness(2, 4, 2, 4);
cboxType.Height = 22;
cboxType.VerticalAlignment = VerticalAlignment.Center;
cboxType.HorizontalAlignment = HorizontalAlignment.Left;
Grid.SetColumn(cboxType, 3);
Grid.SetRow(cboxType, row);
//添加combobox模版
DataTemplate dataTemplate = new DataTemplate();
dataTemplate = (DataTemplate)XamlReader.Load
(@"
);
cboxType.ItemTemplate = dataTemplate;
FieldGrid.Children.Add(cboxType); //FieldGrid是Grid的name值。(