Grid of WPF (1)

Source: Internet
Author: User

The use of the grid on the WPF interface is a little troublesome. It is very different from winform.

Since the start of WPF programming, it has caused a lot of troubles. It is said that the page layout of some technical staff who lack art cells has become very ugly.

Maybe I am a similar person.

However, I think that even if our artist has a good foundation and the interface design is excellent, we still need to dynamically generate the interface, that is, to facilitate the creation and addition of code.

Now, when I first learned Java in college, I used notepad to write a Java Applet, only when the Code had no interface.

Okay. This article only describes how to create the grid code in WPF .. As for others, I will write them in later articles.

The width and height of rows and columns of a grid are not directly defined by numerical values, but by defining a gridlength.

Gridunittype specifies the definition form of gridlength,

 

Gridunittype Member name Description
Auto The size is determined by the size attribute of the content object.
Pixel This value is represented as pixel.
Star This value indicates the weighted ratio of available space.

StarThe size adjustment is used to allocate available space according to the weighted ratio. For more information about this type of size adjustment, see the example of using a star size adjustment.

In eXtensible Application Markup Language (XAML), the asterisk value is represented as * or 2 *. In the first case, the row or column gets twice the available space; in the second case, the row or column gets twice the available space, and so on.

 StarThe size adjustment is used to allocate available space according to the weighted ratio.

In eXtensible Application Markup Language (XAML), the asterisk value is represented as * or 2 *. In the first case, the row or column gets twice the available space; in the second case, the row or column gets twice the available space, and so on.

The following code implements

1 define a grid,

2. divide it into four points on average.

3. How to add control to it.

 Protected void initform () <br/>{< br/> grid = new grid (); <br/> columndefinition col1 = new columndefinition (); <br/> col1.width = new gridlength (this. width/2, gridunittype. star); <br/> columndefinition col2 = new columndefinition (); <br/> col2.width = col1.width; <br/> rowdefinition row1 = new rowdefinition (); <br/> row1.height = new gridlength (this. height/2, gridunittype. star); <br/> rowdefinition row2 = new rowdefinition (); <br/> row2.height = row1.height; <br/> grid. columndefinitions. add (col1); <br/> grid. columndefinitions. add (col2); <br/> grid. rowdefinitions. add (row1); <br/> grid. rowdefinitions. add (row2); </P> <p> // controls <br/> stackpanel SP1 = new stackpanel (); <br/> virtualizingstackpanel VSP = new virtualizingstackpanel (); <br/> dockpanel dp = new dockpanel (); <br/> wrappanel Wp = new wrappanel (); <br/> VSP. background = brushes. blue; <br/> WP. background = brushes. yellow; <br/> DP. background = brushes. green; <br/> sp1.background = brushes. red; </P> <p> // Add control to grid <br/> grid. children. add (VSP); <br/> grid. children. add (DP); <br/> grid. children. add (WP); <br/> grid. children. add (SP1); <br/> // set control's position in the grid </P> <p> // DP position <br/> grid. setcolumn (DP, 0); <br/> grid. setrow (DP, 0); <br/> // <br/> grid. setcolumn (SP1, 1); <br/> grid. setrow (SP1, 0); <br/> // <br/> grid. setcolumn (WP, 0); <br/> grid. setrow (WP, 1); </P> <p>/VSP position <br/> grid. setcolumn (VSP, 1); <br/> grid. setrow (VSP, 1); </P> <p> This. addchild (GRID); <br/>}

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.