翼度科技»论坛 编程开发 .net 查看内容

WPF 入门笔记 - 02 - 布局综合应用

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
本篇博文对接上篇末尾处WPF常用布局控件的综合应用,为痕迹g布局控件介绍课后作业的一个思路方法。
前言

首先来谈一谈布局原则:
WPF窗口只能包含一个元素(Window元素属于内容控件,内容控件只允许有一个子元素),所以我们得在窗口中放置一个容器,才能使我们的窗口放置更多的内容。
所以在WPF中,布局由容器决定,使用容器布局需要注意以下几点:

  • 不要显示设置元素的尺寸:可以通过设置最大和最小尺寸来限定范围。
  • 不要使用屏幕坐标来指定元素位置:根据元素在那种容器中,来合理安排元素的位置。如需要元素之间留白,可以使用Margin设置边距。
  • 可以嵌套布局容器:新建WPF程序会默认提供一个Grid容器,但是我们仍可在Grid中添加容器来安排我们的布局。
一般的布局流程可以概括为以下几步:

  • 确定布局需求:根据UI设计和功能需求,确定所需的布局方式和控件组织结构。
  • 选择合适的布局容器:根据布局需求选择适合的布局容器,如Grid、StackPanel、WrapPanel等。
  • 设置布局属性:使用布局属性控制控件在容器中的位置、大小、对齐方式等。
  • 嵌套布局容器:根据需要,嵌套多个布局容器以实现复杂的布局效果。
  • 调试和优化布局:使用布局调试工具,如布局边框和布局分隔器,对布局进行调试和优化,确保布局效果符合预期。
常用布局面板回顾:
NameDescriptionGrid根据一个不可见的表格在行和列中安排元素,最灵活容器之一。StackPanel在水平或垂直的堆栈中放置元素,多用于复杂窗口中的一些小区域。WrapPanel自适应款高度并自动换行DockPanel根据容器的整个边界调整元素UniformGrid 简化版,强制所有单元格具有相同尺寸。Canvas最基本的面板,只是一个存储控件的容器,使用固定的坐标绝对定位元素常用布局属性:
PropertyDescriptionHorizontalAlignment用于设置子元素在容器中的水平位置 - Center、Left、Right、StretchVerticalAlignment用于设置子元素在容器中的垂直位置 - Center、Top、Bottom、StretchMargin用于指定元素与其父级或同级之间的距离 - 4个方向的边距(左、上、右、下)使用,可以同时设置4个相同边距、也可以单独设置每条边的边距Padding用于指定元素与其子级之间的距离 - 4个方向的边距(左、上、右、下)使用,可以同时设置4个相同边距、也可以单独设置每条边的边距布局详解

作业页面布局如下:

大致上可以划分为图示中的两行一列的布局,然后再细化(这里为了方便查看,我把表格线显示了出来):
  1. <Window x:
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         xmlns:local="clr-namespace:HELLOWPF"
  7.         mc:Ignorable="d"
  8.         Title="MainWindow" Height="450" Width="800">
  9.     <Grid ShowGridLines="True">
  10.         <Grid.RowDefinitions>
  11.             <RowDefinition Height="*"/>
  12.             <RowDefinition Height="8*"/>
  13.         </Grid.RowDefinitions>
  14.         <Border Background="AntiqueWhite"/>
  15.         <Grid Grid.Row="1" ShowGridLines="True">
  16.             <Grid.ColumnDefinitions>
  17.                 <ColumnDefinition Width="4*"/>
  18.                 <ColumnDefinition Width="6*"/>
  19.             </Grid.ColumnDefinitions>
  20.         </Grid>
  21.     </Grid>
  22. </Window>
复制代码
然后完成子元素的布局,左边是个四行两列的布局:
  1. <Grid Grid.Column="0" ShowGridLines="True">
  2.     <Grid.RowDefinitions>
  3.         <RowDefinition/>
  4.         <RowDefinition/>
  5.         <RowDefinition/>
  6.         <RowDefinition/>
  7.     </Grid.RowDefinitions>
  8.     <Grid.ColumnDefinitions>
  9.         <ColumnDefinition/>
  10.         <ColumnDefinition/>
  11.     </Grid.ColumnDefinitions>
  12.     <Border Margin="10" Background="#219afd"/>
  13.     <Border Margin="10" Background="#61b721" Grid.Row="0" Grid.Column="1"/>
  14.     <Border Margin="10" Background="AntiqueWhite" Grid.Row="1" Grid.ColumnSpan="2"/>
  15.     <Border Margin="10" Background="AntiqueWhite" Grid.Row="2" Grid.Column="0"/>
  16.     <Border Margin="10" Background="AntiqueWhite" Grid.Row="2" Grid.Column="1"/>
  17.     <Border Margin="10" Background="AntiqueWhite" Grid.Row="3" Grid.Column="0"/>
  18.     <Border Margin="10" Background="AntiqueWhite" Grid.Row="3" Grid.Column="1"/>
  19. </Grid>
复制代码

右边是个四行三列的布局,也可以通过设置行高的比例设计为三行三列的布局:
  1. <Grid Grid.Column="0" ShowGridLines="True">
  2.     <Grid.RowDefinitions>
  3.         <RowDefinition/>
  4.         <RowDefinition/>
  5.         <RowDefinition/>
  6.         <RowDefinition/>
  7.     </Grid.RowDefinitions>
  8.     <Grid.ColumnDefinitions>
  9.         <ColumnDefinition/>
  10.         <ColumnDefinition/>
  11.     </Grid.ColumnDefinitions>
  12.     <Border Margin="10" Background="#219afd"/>
  13.     <Border Margin="10" Background="#61b721" Grid.Row="0" Grid.Column="1"/>
  14.     <Border Margin="10" Background="AntiqueWhite" Grid.Row="1" Grid.ColumnSpan="2"/>
  15.     <Border Margin="10" Background="AntiqueWhite" Grid.Row="2" Grid.Column="0"/>
  16.     <Border Margin="10" Background="AntiqueWhite" Grid.Row="2" Grid.Column="1"/>
  17.     <Border Margin="10" Background="AntiqueWhite" Grid.Row="3" Grid.Column="0"/>
  18.     <Border Margin="10" Background="AntiqueWhite" Grid.Row="3" Grid.Column="1"/>
  19. </Grid>
复制代码
这样就初步完成了这个页面的布局:
  1. <Window x:
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         xmlns:local="clr-namespace:HELLOWPF"
  7.         mc:Ignorable="d"
  8.         Title="MainWindow" Height="450" Width="800">
  9.     <Grid ShowGridLines="True">
  10.         <Grid.RowDefinitions>
  11.             <RowDefinition Height="*"/>
  12.             <RowDefinition Height="8*"/>
  13.         </Grid.RowDefinitions>
  14.         <Border Background="AntiqueWhite"/>
  15.         <Grid Grid.Row="1" ShowGridLines="True">
  16.             <Grid.ColumnDefinitions>
  17.                 <ColumnDefinition Width="4*"/>
  18.                 <ColumnDefinition Width="6*"/>
  19.             </Grid.ColumnDefinitions>
  20.         </Grid>
  21.     </Grid>
  22. </Window><Window x:
  23.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  24.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  25.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  26.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  27.         xmlns:local="clr-namespace:HELLOWPF"
  28.         mc:Ignorable="d"
  29.         Title="MainWindow" Height="450" Width="800">
  30.     <Grid ShowGridLines="True">
  31.         <Grid.RowDefinitions>
  32.             <RowDefinition Height="*"/>
  33.             <RowDefinition Height="8*"/>
  34.         </Grid.RowDefinitions>
  35.         <Border Background="AntiqueWhite"/>
  36.         <Grid Grid.Row="1" ShowGridLines="True">
  37.             <Grid.ColumnDefinitions>
  38.                 <ColumnDefinition Width="4*"/>
  39.                 <ColumnDefinition Width="6*"/>
  40.             </Grid.ColumnDefinitions>
  41.         </Grid>
  42.     </Grid>
  43. </Window><Window x:
  44.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  45.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  46.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  47.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  48.         xmlns:local="clr-namespace:HELLOWPF"
  49.         mc:Ignorable="d"
  50.         Title="MainWindow" Height="450" Width="800">
  51.     <Grid ShowGridLines="True">
  52.         <Grid.RowDefinitions>
  53.             <RowDefinition Height="*"/>
  54.             <RowDefinition Height="8*"/>
  55.         </Grid.RowDefinitions>
  56.         <Border Background="AntiqueWhite"/>
  57.         <Grid Grid.Row="1" ShowGridLines="True">
  58.             <Grid.ColumnDefinitions>
  59.                 <ColumnDefinition Width="4*"/>
  60.                 <ColumnDefinition Width="6*"/>
  61.             </Grid.ColumnDefinitions>
  62.         </Grid>
  63.     </Grid>
  64. </Window><Window x:
  65.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  66.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  67.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  68.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  69.         xmlns:local="clr-namespace:HELLOWPF"
  70.         mc:Ignorable="d"
  71.         Title="MainWindow" Height="450" Width="800">
  72.     <Grid ShowGridLines="True">
  73.         <Grid.RowDefinitions>
  74.             <RowDefinition Height="*"/>
  75.             <RowDefinition Height="8*"/>
  76.         </Grid.RowDefinitions>
  77.         <Border Background="AntiqueWhite"/>
  78.         <Grid Grid.Row="1" ShowGridLines="True">
  79.             <Grid.ColumnDefinitions>
  80.                 <ColumnDefinition Width="4*"/>
  81.                 <ColumnDefinition Width="6*"/>
  82.             </Grid.ColumnDefinitions>
  83.         </Grid>
  84.     </Grid>
  85. </Window><Window x:
  86.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  87.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  88.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  89.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  90.         xmlns:local="clr-namespace:HELLOWPF"
  91.         mc:Ignorable="d"
  92.         Title="MainWindow" Height="450" Width="800">
  93.     <Grid ShowGridLines="True">
  94.         <Grid.RowDefinitions>
  95.             <RowDefinition Height="*"/>
  96.             <RowDefinition Height="8*"/>
  97.         </Grid.RowDefinitions>
  98.         <Border Background="AntiqueWhite"/>
  99.         <Grid Grid.Row="1" ShowGridLines="True">
  100.             <Grid.ColumnDefinitions>
  101.                 <ColumnDefinition Width="4*"/>
  102.                 <ColumnDefinition Width="6*"/>
  103.             </Grid.ColumnDefinitions>
  104.         </Grid>
  105.     </Grid>
  106. </Window><Window x:
  107.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  108.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  109.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  110.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  111.         xmlns:local="clr-namespace:HELLOWPF"
  112.         mc:Ignorable="d"
  113.         Title="MainWindow" Height="450" Width="800">
  114.     <Grid ShowGridLines="True">
  115.         <Grid.RowDefinitions>
  116.             <RowDefinition Height="*"/>
  117.             <RowDefinition Height="8*"/>
  118.         </Grid.RowDefinitions>
  119.         <Border Background="AntiqueWhite"/>
  120.         <Grid Grid.Row="1" ShowGridLines="True">
  121.             <Grid.ColumnDefinitions>
  122.                 <ColumnDefinition Width="4*"/>
  123.                 <ColumnDefinition Width="6*"/>
  124.             </Grid.ColumnDefinitions>
  125.         </Grid>
  126.     </Grid>
  127. </Window>                       
复制代码

来源:https://www.cnblogs.com/BoiledYakult/archive/2023/05/28/17438548.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具