当前位置:网站首页>WPF study notes "WPF Layout Basics"
WPF study notes "WPF Layout Basics"
2022-07-30 04:34:00 【A solar storm】
WPF布局基础
综述:WPFSaid technology is a kind of page,WPFReasonable layout is using some layout in the container to the layout of the need to display the contents of the,在布局时,Use the controls is one of the most <Grid><StackPanel> Secondly some of the controls like <DockPanel>, <WrapPanel> Less use of it,很多需求 <Grid><StackPanel> 就可以完成啦, <DockPanel> 其实用 <Grid><StackPanel> 来表示.
一、 Grid 控件
1. Grid介绍
Grid As the name suggests is the grid layout,Divide the grid into a a grid,Each grid is to populate the content we,当然啦,Who is going to divide the grid yet?That's right is you,That how to position to each grid yet?Can use the coordinates,Do you want to is a two-dimensional array of that kind of locating way is(如下图).没错,Is the layout position,You may be thinking again,The grid size is divide the large areas of?没错.The default is divide,Now that the default word can special specifies the,那没错了,到这里,We will to a problem,Now that can specify,It must also automatically.没错,Can set the automatic adaptive control size.
2. 代码实例
- 第一步,划分格子
//第一步,划分格子
<Window x:Class="xx_MSS.Pages.InstrumentPages.StatisticInfoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:xx_MSS.Pages.InstrumentPages"
mc:Ignorable="d"
Title="Instrument statistics" Height="450" Width="800">
<Grid>
<!--列定义-->
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!---行定义-->
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
</Grid>
</Window>
就是这种效果
- 第二步、放置元素
<Window x:Class="xx_MSS.Pages.InstrumentPages.StatisticInfoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:xx_MSS.Pages.InstrumentPages"
mc:Ignorable="d"
Title="Instrument statistics" Height="450" Width="800">
<Grid>
<!--列定义-->
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!---行定义-->
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Content="第一个按钮" Grid.Column="0" Grid.Row="0"/>
<Button Content="第二个按钮" Grid.Column="0" Grid.Row="1"/>
<Button Content="第三个按钮" Grid.Column="1" Grid.Row="0"/>
<Button Content="第四个按钮" Grid.Column="1" Grid.Row="1"/>
</Grid>
</Window>
就是这种效果,很好理解吧,This size is specified it,That we ourselves can also specify
- 第三步、Specified percentage width
<Window x:Class="xx_MSS.Pages.InstrumentPages.StatisticInfoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:xx_MSS.Pages.InstrumentPages"
mc:Ignorable="d"
Title="Instrument statistics" Height="450" Width="800">
<Grid>
<!--列定义-->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!---行定义-->
<Grid.RowDefinitions>
<RowDefinition Height="45*" />
<RowDefinition Height="374*"/>
</Grid.RowDefinitions>
<Button Content="第一个按钮" Grid.Column="0" Grid.Row="0"/>
<Button Content="第二个按钮" Grid.Column="0" Grid.Row="1"/>
<Button Content="第三个按钮" Grid.Column="1" Grid.Row="0"/>
<Button Content="第四个按钮" Grid.Column="1" Grid.Row="1"/>
</Grid>
</Window>
就是这种效果,You can specify a specific value,可以用 1* 2* Said value ratio is 1 :2 之类的
- 第四步,Layout from adapted to control the width
<Window x:Class="xx_MSS.Pages.InstrumentPages.StatisticInfoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:xx_MSS.Pages.InstrumentPages"
mc:Ignorable="d"
Title="Instrument statistics" Height="450" Width="800">
<Grid>
<!--列定义-->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!---行定义-->
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Height="150" Width="120" Content="第一个按钮" Grid.Column="0" Grid.Row="0"/>
<Button Content="第二个按钮" Grid.Column="0" Grid.Row="1"/>
<Button Content="第三个按钮" Grid.Column="1" Grid.Row="0"/>
<Button Content="第四个按钮" Grid.Column="1" Grid.Row="1"/>
</Grid>
</Window>

二、StackPanel 控件
1、StackPanel 介绍
StackPanel Layout is actually is the stack layout,He tends to part,The direction of the horizontal or vertical arrangement,Where the content order is through we specified order,当然,Is the layout of the elements inside actually filled with the layout in the form of filling control,That said we specify each layout element alignment,

2、代码实例
- 第一种:默认垂直布局,
<Window x:Class="xx_MSS.Pages.InstrumentPages.StatisticInfoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:xx_MSS.Pages.InstrumentPages"
mc:Ignorable="d"
Title="Instrument statistics" Height="450" Width="800">
<StackPanel>
<Button Content="第一个按钮"/>
<Button Content="第二个按钮" />
<Button Content="第三个按钮" />
<Button Content="第四个按钮" />
</StackPanel>
</Window>

- 第二种 水平布局
<Window x:Class="xx_MSS.Pages.InstrumentPages.StatisticInfoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:xx_MSS.Pages.InstrumentPages"
mc:Ignorable="d"
Title="Instrument statistics" Height="450" Width="800">
<StackPanel Orientation="Horizontal">
<Button Content="第一个按钮"/>
<Button Content="第二个按钮" />
<Button Content="第三个按钮" />
<Button Content="第四个按钮" />
</StackPanel>
</Window>

- 第三种:填充内容
<Window x:Class="xx_MSS.Pages.InstrumentPages.StatisticInfoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:xx_MSS.Pages.InstrumentPages"
mc:Ignorable="d"
Title="Instrument statistics" Height="450" Width="800">
<StackPanel Orientation="Horizontal">
<Button VerticalAlignment="Top" Content="第一个按钮"/>
<Button VerticalAlignment="Center" Content="第二个按钮" />
<Button VerticalAlignment="Bottom" Content="第三个按钮" />
<Button VerticalAlignment="Stretch" Content="第四个按钮" />
<Button Content="第五个按钮" />
</StackPanel>
</Window>

- 第四种,StackPanelWon't go to the content is more than the window
<Window x:Class="xx_MSS.Pages.InstrumentPages.StatisticInfoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:xx_MSS.Pages.InstrumentPages"
mc:Ignorable="d"
Title="Instrument statistics" Height="450" Width="100">
<StackPanel Orientation="Horizontal">
<Button Content="第一个按钮" DockPanel.Dock="Top"/>
<Button Content="第二个按钮" DockPanel.Dock="Left"/>
<Button Content="第三个按钮" DockPanel.Dock="Bottom"/>
<Button Content="第四个按钮" DockPanel.Dock="Right"/>
<Button Content="第五个按钮" />
</StackPanel>
</Window>

三、 DockPanel 控件
1、DockPanel 介绍
Dock is similar to the window that style、Anyway, keep in mind is to write the finish all the corresponding position of,Parked position is all around,There is no central dock that saying
2、代码实例
- 第一种
<Window x:Class="xx_MSS.Pages.InstrumentPages.StatisticInfoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:xx_MSS.Pages.InstrumentPages"
mc:Ignorable="d"
Title="Instrument statistics" Height="450" Width="800">
<DockPanel>
<Button Content="第一个按钮" DockPanel.Dock="Top"/>
<Button Content="第二个按钮" DockPanel.Dock="Left"/>
<Button Content="第三个按钮" DockPanel.Dock="Right"/>
<Button Content="第四个按钮" DockPanel.Dock="Bottom"/>
<Button Content="第五个按钮" />
</DockPanel>
</Window>

Inside place in,May effect is not the same
<Window x:Class="xx_MSS.Pages.InstrumentPages.StatisticInfoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:xx_MSS.Pages.InstrumentPages"
mc:Ignorable="d"
Title="Instrument statistics" Height="450" Width="800">
<DockPanel>
<Button Content="第一个按钮" DockPanel.Dock="Top"/>
<Button Content="第二个按钮" DockPanel.Dock="Left"/>
<Button Content="第三个按钮" DockPanel.Dock="Bottom"/>
<Button Content="第四个按钮" DockPanel.Dock="Right"/>
<Button Content="第五个按钮" />
</DockPanel>
</Window>

四、 WrapPanel 控件
1、WrapPanel 介绍
WrapPanel其实和StackPanel很类似,Also there are horizontal and vertical layout,默认是水平,只是说,StackPanelDon't automatically change the layout,WrapPanelIn window internal elements too much,Automatic transfer element to the next line,
2、代码实例
- 第一种
<Window x:Class="xx_MSS.Pages.InstrumentPages.StatisticInfoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:xx_MSS.Pages.InstrumentPages"
mc:Ignorable="d"
Title="Instrument statistics" Height="450" Width="700">
<WrapPanel>
<Button Content="第一个按钮" DockPanel.Dock="Top"/>
<Button Content="第二个按钮" DockPanel.Dock="Left"/>
<Button Content="第三个按钮" DockPanel.Dock="Bottom"/>
<Button Content="第四个按钮" DockPanel.Dock="Right"/>
<Button Content="第五个按钮" />
</WrapPanel>
</Window>

Content excess window size
<Window x:Class="xx_MSS.Pages.InstrumentPages.StatisticInfoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:xx_MSS.Pages.InstrumentPages"
mc:Ignorable="d"
Title="Instrument statistics" Height="450" Width="300">
<WrapPanel>
<Button Content="第一个按钮" DockPanel.Dock="Top"/>
<Button Content="第二个按钮" DockPanel.Dock="Left"/>
<Button Content="第三个按钮" DockPanel.Dock="Bottom"/>
<Button Content="第四个按钮" DockPanel.Dock="Right"/>
<Button Content="第五个按钮" />
</WrapPanel>
</Window>

边栏推荐
- 2.6归并排序
- Xiamen SenseCore Technology MC3172(1): Introduction and Environment Construction
- handler+message [message mechanism]
- Discourse 自定义头部链接(Custom Header Links)
- cnpm installation steps
- 数据库概论 - MySQL的简单介绍
- VUX Datetime 组件compute-days-function动态设置日期列表
- js operation to add or subtract from the current date (day, week, month, year)
- Notes on "The Law of Construction"---Chapter 10 Typical Users and Scenarios
- Solve the error SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xb7 in position 0: invalid start b
猜你喜欢

商品管理系统数据库设计--SQL Server

Data Lake: Data Integration Tool DataX

Repetition XXL - JOB scheduling center background arbitrary command execution

Introduction to Thymeleaf

The leap second that may cause the next "Millennium Bug" is boycotted by tech giants

MYSQL 唯一约束

网页元素解析a标签

VUX Datetime 组件compute-days-function动态设置日期列表

Android Studio implements login registration - source code (connecting to MySql database)

PyG builds R-GCN to realize node classification
随机推荐
QT(39)-vs开发qt程序提示无法打开源文件
【Untitled】
Learning of redis_Basic part
Go书籍大全-从初级到高级以及Web开发
MySQL operation statement Daquan (detailed)
My first experience of Go+ language——Blessing message system, so that she can also feel your blessings
swagger usage tutorial - quick use of swagger
成为一个合格的网安,你知道这些吗?
Weight line segment tree + line segment tree split/merge + CF1659D
Android Studio implements login registration - source code (connecting to MySql database)
KubeMeet Registration | The complete agenda of the "Edge Native" Online Technology Salon has been announced!
Machine Learning: Knowing the Dimensionality Reduction Process Through Low Variance Filtering
Shanxi group (enterprises) in the second network security skills competition part problem WP (8)
What is the data directory?Why do you need it?
需求设计文档和产品经理的角色改变
文件系统二
Android Studio 实现登录注册-源代码 (连接MySql数据库)
权值线段树+线段树分裂/合并+CF1659D
SSM框架简单介绍
Introduction to Thymeleaf