Some layout elements are often used when designing the Client interface. Grid StackPanel Canvas WrapPanel, etc.
Here's a case study of the scenario where grid is used with WrapPanel.
1, such as on this layout, in the case of unknown number of child elements, we should use WrapPanel to layout, the child elements will wrap themselves.
2, then each line of elements, the first to be left, the second to be centered, the third to reside. This should be laid out through a grid, add three columns, and the corresponding elements will be horizontally oriented in each column.
Here's the code:
1. Interface:
<Windowx:class= "MainWindow"xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"Title= "MainWindow"Height= "+"Width= "$"Loaded= "mainwindow_onloaded"windowstartuplocation= "Centerscreen"> <window.resources> </window.resources> <Grid> <ScrollViewer> <WrapPanelx:name= "Mywrappanel"Margin= "10,0"> </WrapPanel> </ScrollViewer> </Grid></Window>
2, Backstage (language is VB, in fact, VB and C # almost)
ClassMainWindowPrivate SubMainwindow_onloaded (Sender as ObjectE asRoutedEventArgs)DimGrid asGridDimindex =0 forI as Integer=0 to Ten IfIndexMod 3=0 ThenGrid=Newgrid () grid. Width= thegrid. Margin=NewThickness (0, -,0, -) grid. Columndefinitions.add (Newcolumndefinition ()) grid. Columndefinitions.add (Newcolumndefinition ()) grid. Columndefinitions.add (Newcolumndefinition ()) MyWrapPanel.Children.Add (GRID)End If DimCheckBox =NewCheckBox ()DimValue = IndexMod 3 IfValue =0 Thencheckbox.horizontalalignment=HorizontalAlignment.LeftElseIfValue =1 Thencheckbox.horizontalalignment=HorizontalAlignment.CenterElseIfValue =2 Thencheckbox.horizontalalignment=HorizontalAlignment.RightEnd IfControls.Grid.SetColumn (CheckBox, value) Grid. Children.add (checkBox) Index+=1 Next End SubEnd Class
WPF WrapPanel and grid layouts