silverlight 在grid下面动态添加行

该博客介绍了如何在Silverlight中动态地向Grid中添加行,并包含TextBlock和TextBox控件。当用户点击按钮时,会创建包含字段名称、类型和序列号的输入行。此外,还提供了动态添加ComboBox及其DataTemplate的方法,用于显示和选择类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

       /// <summary>
        /// 动态添加行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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
                    (@"<DataTemplate  xmlns=""http://schemas.microsoft.com/client/2007"">
                                        <StackPanel Orientation='Horizontal'>
                                            <TextBlock Text='{Binding Name}' Visibility='Collapsed' />
                                            <TextBlock Text='{Binding Text}' VerticalAlignment='Center' />
                                        </StackPanel>
                                    </DataTemplate>"
                    );
                cboxType.ItemTemplate = dataTemplate;
                FieldGrid.Children.Add(cboxType);    //FieldGrid是Grid的name值。(<Grid Name="FieldGrid" ShowGridLines="False" Margin="0,0,0,-1">)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值