WPF uses the C # code in the background to create a Grid,

Source: Internet
Author: User

WPF uses the C # code in the background to create a Grid,

I have just come into contact with WPF, A cainiao. I encountered such a requirement when I was working on a trainer program. I created a new Grid and added it to a ListView, it is required that the Grid and its sub-elements should be assigned a value to the Name attribute in a certain order. If you create a Grid directly using XAML, there are too many duplicate codes, and the Name is duplicated when you create a Grid, C # code creation, although the amount of code is large, can solve this problem well and be widely used. I will post the code below to share with you that the code quality is not high, for more information.

An external function defines the variable Entries for counting. Use Data Binding to make the first few columns of the Grid consistent with the title width defined in the previous XAML.

1 public Grid newgrid () 2 {3 Grid grid = new Grid (); 4 grid. name = "grid" + Entries. toString (); 5 6 RowDefinition row1 = new RowDefinition (); // instantiate a Grid Row 7 ColumnDefinition co1 = new ColumnDefinition (); // instantiate a Grid Column 8 ColumnDefinition co2 = new ColumnDefinition (); 9 ColumnDefinition CO 3 = new ColumnDefinition (); 10 ColumnDefinition co4 = new ColumnDefinition (); 11 ColumnDefinition co5 = new ColumnDefinition (); 12 bytes co6 = new ColumnDefinition (); 13 ColumnDefinition co7 = new ColumnDefinition (); 14 ColumnDefinition co8 = new ColumnDefinition (); 15 // bind the width of the first three columns to the title width defined in XAML 16 Binding binding1 = new Binding (); 17 // set the Binding data source object 18 binding1.ElementName = "titleco1"; 19 // set the source property 20 binding1.Path = new PropertyPath ("Width "); 21 // bind data 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 = "titleo3"; 31 // set the source property 32 binding3.Path = new PropertyPath ("Width "); 33 // bind data 34 co3.SetBinding (ColumnDefinition. widthProperty, binding3); 35 // Add the instantiated rows and columns to the Grid 36 grid. rowDefinitions. add (row1); 37 grid. columnDefinitions. add (co1); 38 grid. columnDefinitions. add (co2); 39 grid. columnDefinitions. add (O3); 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 // instantiate the required control 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 // Add the previously instantiated element to the 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 // after adding, all are stacked in the first column of the first row, and the following is the sorting 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}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.