WPF is a specialized user interface technology, and layout is one of the core features.
Each layout element has its own characteristics and is flexible to use.
The layout elements in WPF are as follows:
Grid: Grid
You can define your own rows and columns and adjust the layout by the number of rows, row heights, and column widths. Similar to table.
The grid has ColumnDefinitions and RowDefinitions properties, which are collections of columndefinition and RowDefinition, respectively.
Indicates how many rows the grid defines.
StackPanel: Stack panel
You can line up a contained element in a vertical or horizontal direction, removing an element that follows the previous element and automatically fills it forward.
StackPanel uses 3 properties to control the layout of internal elements, respectively:
Orientation (Absolute internal element horizontal or vertical accumulation)
HorizontalAlignment (horizontal alignment)
VerticalAlignment (vertical alignment)
Example: Table of options using the StackPanel layout
<Grid> <groupbox header="Please choose an idiom without typos"Borderbrush="Black"margin="5"> <stackpanel margin="5"> <checkbox content="A. Can't wait"/> <checkbox content="A. sighing"/> <checkbox content="A. Unreasonable"/> <checkbox content="A. the woman is quick"/> <checkbox content="A. Can't wait"/> <stackpanel orientation="Horizontal"Horizontalalignment=" Right"> <button content="Clear"Width=" -"margin="5"/> <button content="Confirm"Width=" -"margin="5"/> </StackPanel> </StackPanel> </GroupBox> </Grid>
Effect:
Canvas Canvases
Inner elements can be positioned in pixel-like absolute coordinates, similar to Winfrom
The following example uses canvas instead of grid to design the login interface: (the window layout does not change, the size is fixed.) Otherwise the grid layout is better elastic
<Canvas> <textblock text="User name:"canvas.left=" A"canvas.top=" A"/> <textbox width=" $"canvas.left=" the"height=" at"canvas.top="9"Borderbrush="Black"/> <textblock text="Password:"canvas.left=" A"canvas.top="40.72"height=" -"Width=" $"/> <textbox width=" $"canvas.left=" the"height=" at"canvas.top=" -"Borderbrush="Black"/> <button content="Determine"Width=" the"height=" A"canvas.left=" -"canvas.top=" the"/> <button content="Clear"Width=" the"height=" A"canvas.left="186"canvas.top=" the"/> </Canvas>
Effect:
DockPanel Parking-Test panel
The elements inside the DockPanel are appended to the Dock property, and the data type is the dock enumeration.
The dock enumeration is lift, Top, right, and Bottom.
DockPanel has an important property of type bool LastChildFill, which is true by default.
LastChildFill is true, the dock property of the last element is ignored. This element will fill all the remaining space.
<Grid> <DockPanel> <textbox dockpanel.dock="Top"height=" -"Borderbrush="Black"/> <textbox dockpanel.dock=" Left"Width=" -"Borderbrush="Black"/> <textbox borderbrush="Black"/> </DockPanel> </Grid>
WrapPanel Automatic Folding Line panel
The inner element is automatically wrapped after it is full, similar to a streaming layout.
Wranppanel uses the Orientation property to control the flow extension direction.
Control internal alignment using VerticalAlignment and HorizontalAlignment
PS: Usually the most used is the grid and StackPanel.
WPF UI layouts (layout)