Silverlight动态创建Gird
按道理可以定制任何想要的界面。如图
大气象 void GeneralGrid()
{
Grid grid = new Grid();
//定义两行
grid.RowDefinitions.Insert(0, new RowDefinition() { Height = new GridLength(20) });
grid.RowDefinitions.Insert(1, new RowDefinition() { Height = new GridLength(20) });
//定义两列
grid.ColumnDefinitions.Insert(0, new ColumnDefinition() { Width = new GridLength(50) });
grid.ColumnDefinitions.Insert(1, new ColumnDefinition() { Width = new GridLength(100) });
TextBox txt = new TextBox();
txt.SetValue(Grid.RowProperty, 0);//定义所在行
txt.SetValue(Grid.ColumnProperty, 0);//定义所在列
Button btn = new Button();
btn.Content = "大气象";
btn.SetValue(Grid.RowProperty, 1);
btn.SetValue(Grid.ColumnProperty, 1);
//添加Grid里面的动态控件
grid.Children.Add(txt);
grid.Children.Add(btn);
//设置Grid的外边距
grid.Margin = new Thickness(10, 20, 30, 40);//左上右下,顺时针
spDataList.Children.Add(grid);//将Grid添加到StackPanel中
}
参考一:silverlight动态创建控件及控件事件动态指定(c#)
在写silverlight程序的某些时候难免要动态创建控件或修改控件事件,以下为示例代码: Code
1
2 public partial class EventDemo : UserControl
3 {
4 private int newButtonPosition = 100;
5
6 public EventDemo()
7 {
8 InitializeComponent();
9 //Anthor按钮单击事件
10 Another.Click += new RoutedEventHandler(Another_Click);
11 }
12
13 //Anthor按钮单击后,执行方法
14 void Another_Click(object sender, RoutedEventArgs e)
15 {
16 //创建一个Button
17 Button b = new Button();
18 //显示内容
19 b.Content = "I live!";
20 //为新创建的控件新建Thickness对象,用来设置Button控件的位置
21 //原文中使用了Canvas.LeftProperty和Canvas.TopProperty
22 Thickness tn = new Thickness(10,this.newButtonPosition,0,0);
23 b.SetValue(Canvas.StyleProperty, tn);
24 //到顶部的距离递增
25 this.newButtonPosition += 30;
26 b.Width = 100;
27 b.Height = 20;
28 //给这个新建的按钮的Click事件添加一个处理方法
29 b.Click += new RoutedEventHandler(b_Click);
30 //添加到父控件,并显示
31 myCanvas.Children.Add(b);
32 }
33
34 //点击添加的控件触发
35 void b_Click(object sender, RoutedEventArgs e)
36 {
37 Button btn = sender as Button;
38 btn.Content = "Don't do that!";
39 btn.IsEnabled = false;
40 }
41 }
42
43 参考二:
C#代码:
参考三:
Code below can add item that contain a button and a textbox which are lay in a grid to combobox.
ComboBoxItem item = new ComboBoxItem();
Grid grid = new Grid();
grid.ColumnDefinitions.Insert(0, new ColumnDefinition() { Width = new GridLength(50) });
grid.ColumnDefinitions.Insert(1, new ColumnDefinition() { Width = new GridLength(100) });
Button MyButton = new Button();
MyButton.SetValue(Grid.ColumnProperty, 0);
MyButton.DataContext = "11";
MyButton.Click+=new RoutedEventHandler(MyButton_Click);
TextBox textbox = new TextBox();
textbox.Name = "text";
textbox.SetValue(Grid.ColumnProperty, 1);
grid.Children.Add(MyButton);
grid.Children.Add(textbox);
item.Content = grid;
Combobox1.Items.Add(item);
与50位技术专家面对面20年技术见证,附赠技术全景图
总结
以上是生活随笔为你收集整理的Silverlight动态创建Gird的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 肝上血管瘤会自己消失(肝上血管瘤)
- 下一篇: web开发语言大盘点