WPF beginners (1) & mdash; layout [good interface Basics] And wpf beginner layout Basics

Source: Internet
Author: User

WPF beginners (1)-layout [good interface Basics] And wpf beginners layout Basics

Some people who switch from Winform to WPF may ignore the layout and use fixed positioning habitually. However, there is no good layout, and the interface controls behind the screen are beautiful and fancy. They are all just flowers inserted on the cow dung, and they may always be full of flowers ?? (Haha ).

Let's talk about the most common and powerful layout of WPF-Grid. In many cases, we will only notice the most common feature, however, its power is often ignored. Its power is that it can be replaced with most other layout panels. Of course, it may not be convenient to use layout panels with special requirements.

For example, the Grid layout consists of three rows and three columns, with the same row score. The column ratio is.

<Grid>    <Grid.RowDefinitions>        <RowDefinition Height="*"/>        <RowDefinition Height="*"/>        <RowDefinition Height="*"/>    </Grid.RowDefinitions>    <Grid.ColumnDefinitions>        <ColumnDefinition Width="*"/>        <ColumnDefinition Width="2*"/>        <ColumnDefinition Width="3*"/>    </Grid.ColumnDefinitions></Grid>

In addition to "*", you can also use "auto". The difference between the two is that * proportional [External], auto automatically adjusts the size according to the internal control [internal ]. <StackPanel> <Button Content = "One"/> <Button Content = "Two"/> <Button Content = "Three"/> <Button Content = "Four"/> </StackPanel>

I often like to place another panel with another one -- WrapPanel streaming layout. I think they are just like the TextWrap attribute in TextBlock, so they can be differentiated by line breaks, wrapPanel should be the best understood by students. After all, every student writes his homework and writes a line of his/her composition ].

 

Another commonly used layout panel is the DockPanel. We should know that the Winform control often has the Dock attribute, which is similar to the Winform Dock attribute. It is like a room in the house. Some things are placed on the left side of the wall, while others are on the right side.

 

Since we decided to use wpf, the most important thing to put it down is to use Canvas layout. Of course, its existence still makes sense after all, but it seems like a wooden discovery ....

 

Finally, we need to propose a non-layout panel control, Border. This control can be said to be a layout control of the layout panel, which is often mixed with the layout control. If you need a rounded Border, Border will be used outside a layout panel.


WPF to help with interface layout problems

I have not read the programming book. The implementation is actually very simple. The four buttons are a WrapPanel control, and then the status of the buttons is changed according to the IsCheck attribute in the CheckBox Click event, if the value is True, the Visibility attribute of the button is Visible. If the value is False, the Visibility attribute of the button is Collapsed.

How to customize the WPF layout panel for beginners

  
The WPF layout engine uses A recursive method to implement the layout of controls and their child controls. The general process is as follows: to implement the layout of control, first, implement the sub-controls a1, a2, a3... to achieve the a1 layout, you must implement the a11, a12, a13... after the child control layout is complete, the layout of the parent control is completed, and then recursively goes back. After recursion ends, the layout is complete.
The final size and position of the control are determined by the Control and the parent control. The parent control gives the child control a layout space within its capabilities, the child control returns an expected value to the parent control. The parent control finally allocates a certain amount of space to the Child control based on its own space size and the expected value of the Child control and returns its own size. this process is completed by overwriting the MeasureOverride and ArrangeOverride methods of the IC trading network panel.
  
2, Size MeasureOverride (Size availableSize) Method
This method evaluates the space required by the layout control.
Refer to the following code:
The following is a reference clip:
Dimensions (SizeavailableSize) {SizechildrenSize = newSize (); foreach (UIElementchildinthis. Children) {child. Measure (newSize (Double. PositiveInfinity, Double. PositiveInfinity ));
ChildrenSize. Width + = child. DesiredSize. Width;
ChildrenSize. height + = child. desiredSize. height;} returnchildrenSize;} in the MeasureOverride phase, we call the Measure (Size sz) method for each Child in the panel, the function of this method is that the parent control tells the Child control the expected space to be allocated to the Child control. After the Measure method is called for Child, the child control sends a response to the parent control internally to inform the parent control of the expected size, and the expected value of the child control is saved in its child. in DesiredSize. if the positive infinity (new Size (Double. positiveInfinity, Double. positiveInfinity), it is equivalent to the parent Control saying to the Child control, "although you need the size, if you can, the world can give it to you ".
The availableSize parameter in the MeasureOverrice method is the value that is passed in when its parent control (the Panel control you wrote later in actual use) calls the Measure method (minus some boundary values, for example, to remove Margin)
The returned value of the MeasureOverride method is the expected value that it tells its parent control, that is, after the parent control (the Panel control you wrote is actually used in the future) calls the Measure method to it, its DesiredSize Value Note: As a parent control, you can easily meet the needs of child controls (although not necessarily have this capability ), therefore, you can pass in a positive infinity size when calling the Measure method on the Child control. However, as a child control, you cannot greedy the parent Control for positive infinity space, therefore, we cannot regard positive infinity as the return value of the MeasureOverride method, you cannot directly use the availableSize parameter of this method as the return value (because your parent control may pass positive infinity as this parameter to you)
3, Size ArrangeOverride (Size fin... the remaining full text>

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.