A WPF window can contain only one element, in order to place multiple elements in a WPF window and create a more practical user interface, you need to place a container on the window and then place other elements in the container. Note: The reason for this limitation is that the window class inherits from the ContentControl class, saying that the simple layout has to say the WPF layout principle: The layout principle that the ideal window should follow: 1, the size of the set element that should not be displayed. 2, the position of the element should not be specified using screen coordinates. 3, the layout container and their child elements "share" the space that can be used. 4, you can nest layout containers. The layout process includes: The measurement phase and the arrangement stage. All WPF layout containers are panels that derive from the System.windows.controls.panel abstract class.
layout container:
1,statckpanel: Places an element in a horizontal or vertical stack.
<window x:class= "layout. MainWindow "
xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
title= "layout test" height= "width=" 525 ">
<stackpanel orientation= "Vertical" >
<!--orientation Layout direction
--
<label height= "> A button stack</label>
<button height= "WU" >button1</Button>
<button height= ">button2</Button>
"
<button height= "WU" >button3</Button>
<button height= ">button4</Button>
"
<button height= ">button5</Button>
"
</StackPanel>
</Window>
2,wrappanel: Places elements in a series of lines that can be wrapped, and horizontally, the WrapPanel panel places entries in a left-to-right manner, and then places the elements in the subsequent rows, vertically, The WrapPanel panel places elements in the top-down column and uses additional columns to place the remaining entries.
<window x:class= "layout. WrapPanel panel xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" title= " WrapPanel Panel "height=" width= "309.195" > <wrappanel orientation= "Vertical" > <button content= "Button1" width= "page"/> <button content= "Button2" Width = "/> " <button content= "Button3" width= "page"/> <button Content= "Button4" width= "page"/> <button content= "Button5" width= "page"/> <button content= "Button6" width= "page"/> <button content= "Button7" Width = "/> " <button content= "Button8" width= "page"/> <buttOn content= "Button9" width= "page"/> <button content= "Button10" width= "page"/> & nbsp <button content= "Button11" width= "page"/> <button content= "Button12" Width=/> <button content= "Button13" width= "page"/> < Button content= "Button14" width= "page"/> <button content= "Button15" width= "page"/> <button content= "Button16" width= "page"/> </wrappanel></window >
3,dockpanel: Adjusts elements based on the entire bounds of the container. <window x:class= "layout. DockPanel Panel "
xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.c Om/winfx/2006/xaml "title=" dockpanel Panel "height=" "width=" >
<border margin= "10,10" background= "LightBlue" borderbrush= "Pink" borderthickness= "5" cornerradius= > <dockpanel lastchildfill= "False" > & nbsp <button content= "Top1" dockpanel.dock= "Top" ></Button> < Button content= "left1" dockpanel.dock= "left" ></Button> <button Content= "right1" dockpanel.dock= "right" ></Button> <button Content = "Bottom1" dockpanel.dock= "Bottom" ></Button> </DockPanel> < /border></window>
4,grid: The most common elements, not much like the table layout elements in HTML. One of the most flexible layout containers for table layouts. Cell is set to digital * and the container is scaled proportionally . <window x:class= "layout. Grid Top Container "
xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" title= "grid top-level container" height= " "Width=" > <grid showgridlines= "True" > <grid.rowdefinitions > <RowDefinition></RowDefinition> & nbsp <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition> </ColumnDefinition> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> & lt;/grid.columndefinitions> <BUtton content= "TopLeft" grid.row= "0" grid.column= "0" ></Button> <button content= "Middleleft" grid.row= "0" grid.column= "1" ></Button> <button content= " BottomRight "grid.row=" 1 "grid.column=" 2 "></Button> <button content=" Bottommiddle "grid.row=" 1 "grid.column=" 1 "></Button> </Grid></Window>
5,uniformgrid: Places an element in a table that is not visible but forces all cells to have the same size. Not used
<window x:class= "layout. UniformGrid Panel "
xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
title= "uniformgrid Panel" height= "width=" >
<border background= "Lightgray" borderbrush= "Brown" borderthickness= "5,5" cornerradius= "Ten" margin= "5" >
<uniformgrid rows= "2" columns= "2" >
<button>top left</button>
<button>top right</button>
<button>bottom left</button>
<button>bottom right</button>
</UniformGrid>
</Border>
</Window>
6,canvas: is the fixed coordinate absolute positioning element. This layout container is most similar to a traditional Windows Form, but does not provide anchoring or docking functionality and is not recommended for use.
<window x:class= "layout. Canvas Panel "
xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
title= "canvas Panel" height= "width=" >
<border borderbrush= "Beige" borderthickness= "Ten" margin= "5" background= "Yellowgreen" cornerradius= ">
<Canvas>
<button canvas.left= "Ten" canvas.top= "ten" >10,10</Button>
<button canvas.left= "canvas.top=" >20,20</Button>
<button canvas.left= "canvas.top=" >30,40</Button>
<button canvas.right= "canvas.bottom=" >60,50</Button>
</Canvas>
</Border>
</Window>
7,border border
<window x:class= "layout. Border control" xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= " Http://schemas.microsoft.com/winfx/2006/xaml "title=" Border Control "height=" width= "> <border margin=" 7 "Padding=" background= "Lightyellow" borderbrush= "Steelblue" borderthickness= "3,5,3,5" cornerradius= "5" Verticalalignment= "Top" > <StackPanel> <label name= "lb" >One</Label> <b Utton name= "BT2" >Two</Button> <button name= "BT3" >Three</Button> </StackPanel> </Border></Window>
WPF Simple Layout Scratch