I have just contacted WPF, rookie one, in doing a practiced hand program encountered such a requirement, to create a new grid and add it to a ListView, the grid and its child elements should be in a certain order to assign the name attribute, the direct use of XAML created by repeated code too much, and create a new grid when the name is duplicated, C # code creation Although the code is large, but it is a good solution to this problem, can be widely used, the following code to share with you, code quality is not high, need to improve the place also ask you great God to advise.
A variable entries is defined outside the function for counting. Using data binding makes the first few columns of the grid consistent with the width of the headings defined in the previous XAML.
1 PublicGrid Newgrid ()2 {3Grid Grid =NewGrid ();4Grid. Name ="Grid"+entries.tostring ();5 6RowDefinition Row1 =NewRowDefinition ();//instantiate a grid row7ColumnDefinition Co1 =NewColumnDefinition ();//instantiate a grid column8ColumnDefinition CO2 =Newcolumndefinition (); 9ColumnDefinition CO3 =Newcolumndefinition (); TenColumnDefinition Co4 =Newcolumndefinition (); OneColumnDefinition Co5 =Newcolumndefinition (); AColumnDefinition Co6 =Newcolumndefinition (); -ColumnDefinition Co7 =Newcolumndefinition (); -ColumnDefinition Co8 =Newcolumndefinition (); the //binds the width of the first three columns to the width of the caption defined in XAML -Binding binding1 =NewBinding (); - //To set a bound data source object -Binding1. ElementName ="Titleco1"; + //Set Source Properties -Binding1. Path =NewPropertyPath ("Width"); + //for data binding A Co1. SetBinding (Columndefinition.widthproperty, binding1); at -Binding binding2 =NewBinding (); -Binding2. ElementName ="Titleco2"; -Binding2. Path =NewPropertyPath ("Width"); - CO2. SetBinding (Columndefinition.widthproperty, binding2); - inBinding Binding3 =NewBinding (); -Binding3. ElementName ="Titleco3"; to //Set Source Properties +Binding3. Path =NewPropertyPath ("Width"); - //for data binding the Co3. SetBinding (Columndefinition.widthproperty, binding3); * //adding instantiated rows and columns to a grid $ grid. Rowdefinitions.add (ROW1);Panax Notoginseng grid. Columndefinitions.add (CO1); - grid. Columndefinitions.add (CO2); the grid. Columndefinitions.add (CO3); + grid. Columndefinitions.add (CO4); A grid. Columndefinitions.add (CO5); the grid. Columndefinitions.add (CO6); + grid. Columndefinitions.add (CO7); - grid. Columndefinitions.add (CO8); $ $ //instantiating the required controls -CheckBox Ckbchose =NewCheckBox (); -Ckbchose.name ="chose"+entries.tostring (); theTextBox Txtnumber =NewTextBox (); -Txtnumber.name =" Number"+entries.tostring ();WuyiComboBox cmbprocedure =NewComboBox (); theCmbprocedure.name ="cmbprocedure"+entries.tostring (); -TextBox txtcompletion =NewTextBox (); WuTxtcompletion.name ="Completion"+entries.tostring (); -TextBox txtduration =NewTextBox (); AboutTxtduration.name ="Duration"+entries.tostring (); $TextBox Txtstarttime =NewTextBox (); -Txtstarttime.name ="StartTime"+entries.tostring (); -TextBox Txtpredecessor =NewTextBox (); -Txtpredecessor.name ="predecessor"+entries.tostring (); ATextBox Txtresource =NewTextBox (); +Txtresource.name ="Resource"+entries.tostring (); the //adds the previously instantiated element to the grid - grid. Children.add (ckbchose); $ grid. Children.add (txtnumber); the grid. Children.add (cmbprocedure); the grid. Children.add (txtcompletion); the grid. Children.add (txtduration); the grid. Children.add (txtstarttime); - grid. Children.add (txtpredecessor); in grid. Children.add (Txtresource); the //all of the additions are stacked in the first column of the first row, followed by sorting them theGrid.setcolumn (Ckbchose,0); AboutGrid.setcolumn (Txtnumber,1); theGrid.setcolumn (Cmbprocedure,2); theGrid.setcolumn (Txtcompletion,3); theGrid.setcolumn (Txtduration,4); +Grid.setcolumn (Txtstarttime,5); -Grid.setcolumn (Txtpredecessor,6); theGrid.setcolumn (Txtresource,7);Bayi returngrid; the}
WPF creates a grid using background C # code