你的位置:首页 > ASP.net教程

[ASP.net教程]WPF使用后台C#代码创建Grid


笔者刚刚接触WPF,菜鸟一枚,在做一个练手程序时遇到这样一个需求,创建一个新的Grid并将其添加至一个ListView中,要求Grid及其子元素应按一定顺序给Name属性赋值,直接使用XAML创建的话重复代码过多,且新建一个Grid时名称重复,C#代码创建虽然代码量较大,但是很好的解决了这一问题,可以广泛应用,下面就贴出代码与诸位分享一下,代码质量不高,需要改进的地方还请诸位大神多多指教。

在函数外定义了一个变量Entries用于计数。使用数据绑定使得该Grid前几列与之前XAML中定义的标题宽度一致。

 1  public Grid newgrid() 2     { 3       Grid grid = new Grid(); 4       grid.Name = "grid" + Entries.ToString(); 5  6       RowDefinition row1 = new RowDefinition();  //实例化一个Grid行 7       ColumnDefinition co1 = new ColumnDefinition();  //实例化一个Grid列 8       ColumnDefinition co2 = new ColumnDefinition();   9       ColumnDefinition co3 = new ColumnDefinition();  10       ColumnDefinition co4 = new ColumnDefinition();  11       ColumnDefinition co5 = new ColumnDefinition();  12       ColumnDefinition co6 = new ColumnDefinition(); 13       ColumnDefinition co7 = new ColumnDefinition();  14       ColumnDefinition co8 = new ColumnDefinition();  15       //将前三列的宽度与在XAML中定义的标题宽度绑定16       Binding binding1 = new Binding();17       //设置绑定数据源对象18       binding1.ElementName = "titleco1";19       //设置源属性20       binding1.Path = new PropertyPath("Width");21       //进行数据绑定22       co1.SetBinding(ColumnDefinition.WidthProperty, binding1);23 24       Binding binding2 = new Binding();      25       binding2.ElementName = "titleco2";     26       binding2.Path = new PropertyPath("Width");     27       co2.SetBinding(ColumnDefinition.WidthProperty, binding2);28 29       Binding binding3 = new Binding();30       binding3.ElementName = "titleco3";31       //设置源属性32       binding3.Path = new PropertyPath("Width");33       //进行数据绑定34       co3.SetBinding(ColumnDefinition.WidthProperty, binding3);35       //将实例化的行和列添加到Grid中36       grid.RowDefinitions.Add(row1);37       grid.ColumnDefinitions.Add(co1);38       grid.ColumnDefinitions.Add(co2);39       grid.ColumnDefinitions.Add(co3);40       grid.ColumnDefinitions.Add(co4);41       grid.ColumnDefinitions.Add(co5);42       grid.ColumnDefinitions.Add(co6);43       grid.ColumnDefinitions.Add(co7);44       grid.ColumnDefinitions.Add(co8);45 46       //实例化需要的控件47       CheckBox ckbChose = new CheckBox();48       ckbChose.Name = "Chose" + Entries.ToString();49       TextBox txtNumber = new TextBox();50       txtNumber.Name = "number" + Entries.ToString();51       ComboBox cmbProcedure = new ComboBox();52       cmbProcedure.Name = "cmbProcedure" + Entries.ToString();53       TextBox txtCompletion = new TextBox();54       txtCompletion.Name = "Completion" + Entries.ToString();55       TextBox txtDuration = new TextBox();56       txtDuration.Name = "Duration" + Entries.ToString();57       TextBox txtStartTime = new TextBox();58       txtStartTime.Name = "StartTime" + Entries.ToString();59       TextBox txtPredecessor = new TextBox();60       txtPredecessor.Name = "Predecessor" + Entries.ToString();61       TextBox txtResource = new TextBox();62       txtResource.Name = "Resource" + Entries.ToString();63       //将前面实例化的元素添加至Grid64       grid.Children.Add(ckbChose);65       grid.Children.Add(txtNumber);66       grid.Children.Add(cmbProcedure);67       grid.Children.Add(txtCompletion);68       grid.Children.Add(txtDuration);69       grid.Children.Add(txtStartTime);70       grid.Children.Add(txtPredecessor);71       grid.Children.Add(txtResource);72       //添加后全部是堆积在第一行第一列,下面是对其进行排序73       Grid.SetColumn(ckbChose, 0);74       Grid.SetColumn(txtNumber, 1);75       Grid.SetColumn(cmbProcedure, 2);76       Grid.SetColumn(txtCompletion, 3);77       Grid.SetColumn(txtDuration, 4);78       Grid.SetColumn(txtStartTime, 5);79       Grid.SetColumn(txtPredecessor, 6);80       Grid.SetColumn(txtResource, 7);81       return grid;82     }