
WPF布局原则
不应显示设置大小
为了布局的稳定性,控件的大小应该可以自动适应容器。如下为新建一个窗体,默认包含一个Grid容器,该控件没有显示设置宽高,所以,在改变窗体大小的时候,该容器的大小也变变化,但如果显示的加了宽或高,在改变窗体大小后,该容器的大小受限并且位置不再稳定


使用相对坐标
容器的子元素应该以父级位置而去定位,而不是使用窗体的坐标
与子元素共享空间
如果空间允许,布局容器会根据每个元素的内容尽可能为元素设置更合理的尺寸。它们还会向一个或多个子元素分配多余的空间
支持嵌套布局
多种容器可相互嵌套使用,完成最优布局
StackPanel 控件
它是一个布局容器,在单行单列中放置子元素,叫做堆栈面板
在窗体中添加一个StackPanel容器,在工具箱中打到StackPanel控件,将其拖动到窗体上,在这里,StackPanel容器被嵌套在了Grid容器中,也可以将Grid容器删除,单独使用StackPanel容器;StackPanel的默认XMAL标签是个单标签,如果需要在StackPanel中包含子级,需要将其设置为双标签格式

将StackPanel所有的属性先删除掉,然后添加一个标签控件,三个按钮控件,一个文本框控件,效果如下;可以看出,StackPanel默认会将子元素垂直排列

可以通过Orientation属性来控制子元素排列方向
Orientation="Horizontal"
表示水平排列
Orientation="Vertical"
表示垂直排列,它是默认的

HorizontalAlignment属性可以控制子元素水平方向的位置,控制元素是靠左还中靠右等
该属性对应的值有Right/Left/Center/Stretch
,该属性只对于横向伸展(垂直排列)的元素有效果
HorizontalAlignment="Right"
表示将元素水平区于右,结果如下图

VerticalAlignment属性可以控制子元素垂直方向的位置
VerticalAlignment对应的值有Bottom/Center/Stretch/Top
VerticalAlignment="Top"
会将横向分布(垂直伸展)的元素区于上方,如下图

因为子元素的排列方向发生了变化,这里看到,HorizontalAlignment="Right"
已经不再起作用
Margin属性为元素添加外边距
Margin="10,15,20,25"
的四个值分别表示左、上、右、下的位置,如下图

如果Margin属性只有一个值的话表示的是上下位置,如果只有两个值的话,第一个值表示上下,第二个值表示的是左右
MinWidth属性可以控制元素的最小尺寸,也就是说该元素不可以小于设定的最小宽度值
MaxWidth属性可以控制元素的最大尺寸,也就是说该元素不可以大于设定的最大宽度值
Border控件
Border 是一个装饰的控件,用此控件绘制一个边框、一个背景.在 Border 中只能有一个子控件,但它的子控件是可以包含多个子控件的
示例与代码如下

<Border>
<StackPanel Orientation="Vertical">
<Label Content="Label"/>
<Button Content="Button" />
<Button Content="Button" />
<Button Content="Button" />
<TextBox Height="23" TextWrapping="Wrap" Text="TextBox"/>
</StackPanel>
</Border>
从上例可以看出,Border中只包含了一个子元素StackPanel,而StackPanel中包含了多个子元素
BorderThickness属性可以设置Border控件边的宽度,而BorderBrush属性可以设置边的颜色,如下

<Border BorderThickness="16" BorderBrush="BlueViolet">
<StackPanel Orientation="Vertical">
<Label Content="Label"/>
<Button Content="Button" />
<Button Content="Button" />
<Button Content="Button" />
<TextBox Height="23" TextWrapping="Wrap" Text="TextBox"/>
<Border BorderBrush="Black" BorderThickness="1" Height="100"/>
</StackPanel>
</Border>
Padding属性可以为元素设置内填充,效果如下

<Border BorderThickness="16" BorderBrush="BlueViolet" Padding="30">
<StackPanel Orientation="Vertical">
<Label Content="Label"/>
<Button Content="Button" />
<Button Content="Button" />
<Button Content="Button" />
<TextBox Height="23" TextWrapping="Wrap" Text="TextBox"/>
<Border BorderBrush="Black" BorderThickness="1" Height="100"/>
</StackPanel>
</Border>
CornerRadius属性可以为Border控件设置边度的弧度,如下图

<Border BorderThickness="16" BorderBrush="BlueViolet" Padding="30" CornerRadius="45">
<StackPanel Orientation="Vertical">
<Label Content="Label"/>
<Button Content="Button" />
<Button Content="Button" />
<Button Content="Button" />
<TextBox Height="23" TextWrapping="Wrap" Text="TextBox"/>
<Border BorderBrush="Black" BorderThickness="1" Height="100"/>
</StackPanel>
</Border>
WrapPanel面板
WrapPanel容器将子元素按行或列一个接一个进行排列,如果一行或一列放不下,元素会被挤到下一行或一列;同一行元素的高度一样,同一列元素的宽度是一样的
示例如下:

<Window x:Class="WPF_Code.WarpPanel_wpf"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WarpPanel_wpf" Height="300" Width="300">
<WrapPanel>
<Button Content="Button" />
<Button Content="Button" />
<!--这里只设置了一个button的高度,与它同一行的元素都变成了一样的高度-->
<Button Content="Button" Height="30" />
<Button Content="Button" />
<Button Content="Button" />
<Button Content="Button" />
<Button Content="Button" />
<Button Content="Button" />
</WrapPanel>
</Window>

<Window x:Class="WPF_Code.WarpPanel_wpf"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WarpPanel_wpf" Height="300" Width="300">
<!--将WrapPanel元素排列方向改为垂直方向-->
<WrapPanel Orientation="Vertical">
<!--在垂直方向上,如果改变一个元素的高度的话对其它元素没有影响-->
<Button Content="Button" Height="164" />
<Button Content="Button" />
<!--在垂直方向上,如果改变一个元素的宽度则同列上的元素的宽度都会有变化-->
<Button Content="Button" Width="100" />
<Button Content="Button" />
<Button Content="Button" />
<Button Content="Button" />
<Button Content="Button" />
<Button Content="Button" />
</WrapPanel>
</Window>
DockPanel面板
DockPanel支持让元素简单地停靠在整个面板的某一条边上,然后拉伸元素以填满全部宽度或高度。它也支持让一个元素填充其他已停靠元素没有占用的剩余空间
DockPanel有一个Dock附加属性,因此子元素用4个值来控制她们的停靠:Left、Top、Right、Bottom。Dock 没有Fill值。作为替代,最后的子元素将加入一个DockPanel并填满所有剩余的空间,除非DockPanel的LastChildFill属性为false,它将朝某个方向停靠
示例如下:

<Window x:Class="WPF_Code.WarpPanel_wpf"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WarpPanel_wpf" Height="300" Width="300">
<DockPanel>
<Button Content="Button"/>
<Button Content="Button"/>
<Button Content="Button"/>
<Button Content="Button"/>
<Button Content="Button"/>
</DockPanel>
</Window>
默认情况下,元素会依次横向排列,并填充整个空间

<Window x:Class="WPF_Code.WarpPanel_wpf"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WarpPanel_wpf" Height="300" Width="300">
<!--LastChildFill="True"允许最后一个元素填充整个空间,默认是Ture-->
<DockPanel LastChildFill="True">
<!--DockPanel.Dock属性可控制元素在DockPanle中依靠的位置-->
<Button Content="Button" DockPanel.Dock="Top" />
<Button Content="Button" DockPanel.Dock="Bottom"/>
<Button Content="Button" DockPanel.Dock="Left"/>
<Button Content="Button" DockPanel.Dock="Right"/>
<Button Content="Button"/>
</DockPanel>
</Window>
嵌套布局容器
一个简单示例

<Window x:Class="WPF_Code.WarpPanel_wpf"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WarpPanel_wpf" Height="300" Width="300">
<!--一个DockPanel容器,LastChildFill允许最后一个元素填充整个剩余空间-->
<DockPanel LastChildFill="True">
<!--嵌套一个StackPanel容器,将子元素横向排列,并领先到DockPanel的底部,然后居中-->
<StackPanel DockPanel.Dock="Bottom" HorizontalAlignment="Center" Orientation="Horizontal">
<Button Name="btn1" Content="OK" Padding="10"/>
<Button Name="btn2" Content="No" Padding="10"/>
</StackPanel>
<!--DockPanel的最后一个元素,默认填充整个剩余空间-->
<TextBox DockPanel.Dock="Top">文本输入……</TextBox>
</DockPanel>
</Window>
注:可以通过文档大纲栏来查看元素节点树状图

Grid容器
Grid控件作为WPF中最为重要的布容器
调整行与列
通过Grid.RowDefinitions来定义行,Grid.ColumnDefinitions来定义列

<Window x:Class="WPF_Code.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<!--Grid.RowDefinitions定义行-->
<Grid.RowDefinitions>
<RowDefinition></RowDefinition><!--一行-->
<RowDefinition></RowDefinition><!--一行-->
</Grid.RowDefinitions>
<!--Grid.ColumnDefinitions定义列-->
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition><!--一列-->
<ColumnDefinition></ColumnDefinition><!--一列-->
</Grid.ColumnDefinitions>
</Grid>
</Window>
上例中定义了两行两列,因此Grid被分割成了四个部分
布局舍入
Grid的 宽度和高度设置的三种方法:
-
指定数值 如: Width = "50" ,不建议使用此方式
-
等比例设置: Width="*" Width = "2*" Width = "3*"
这样的话,三个单元则以1:2:3 比例设置大小

<Window x:Class="WPF_Code.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<!--Grid.RowDefinitions定义行-->
<Grid.RowDefinitions>
<RowDefinition></RowDefinition><!--一行-->
<RowDefinition></RowDefinition><!--一行-->
</Grid.RowDefinitions>
<!--Grid.ColumnDefinitions定义列-->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition><!--一列-->
<ColumnDefinition Width="2*"></ColumnDefinition><!--一列-->
<ColumnDefinition Width="3*"></ColumnDefinition><!--一列-->
</Grid.ColumnDefinitions>
</Grid>
</Window>
- 自适应: Width = “Auto” 单元格适应内部控件大小
也就是说,元素的大小多大,分割的部分就会自动变成多大

<Window x:Class="WPF_Code.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<!--Grid.RowDefinitions定义行-->
<Grid.RowDefinitions>
<RowDefinition></RowDefinition><!--一行-->
<RowDefinition Height="Auto"></RowDefinition><!--一行-->
</Grid.RowDefinitions>
<!--Grid.ColumnDefinitions定义列-->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition><!--一列-->
<ColumnDefinition Width="2*"></ColumnDefinition><!--一列-->
<ColumnDefinition Width="3*"></ColumnDefinition><!--一列-->
</Grid.ColumnDefinitions>
<!--在第二行第一列上加一个按钮-->
<Button Grid.Column="0" Grid.Row="1">你好</Button>
</Grid>
</Window>
当Grid的大小按比例模式进行分割时,可能某一部分的大小不是一个整数,此时如果在该部分加一个整数大小像素的图片,就会变得模糊,这是由于WPF的混合锯齿是的最小单元格的界面显示模糊,为了解决这种情况,则需要设置Grid 的UseLayoutRounding 属性,以增加抗锯齿效果
<Grid UseLayoutRounding="True">
将元素跨行或跨列安放
ColumnSpan 属性表示跨列
RowSpan 属性表示跨行
示例代码<Button Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Grid.ColumnSpan="2">你好</Button>
,如下图

<Window x:Class="WPF_Code.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid UseLayoutRounding="True">
<!--Grid.RowDefinitions定义行-->
<Grid.RowDefinitions>
<RowDefinition></RowDefinition><!--一行-->
<RowDefinition ></RowDefinition><!--一行-->
</Grid.RowDefinitions>
<!--Grid.ColumnDefinitions定义列-->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition><!--一列-->
<ColumnDefinition Width="2*"></ColumnDefinition><!--一列-->
<ColumnDefinition Width="3*"></ColumnDefinition><!--一列-->
</Grid.ColumnDefinitions>
<!--在第二行第一列上加一个按钮-->
<Button Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Grid.ColumnSpan="2">你好</Button>
</Grid>
</Window>
分割容器
GridSplitter 控件就是用来分割Grid控件的,必须放在Grid里面
示例代码<GridSplitter Grid.Column="1" VerticalAlignment="Stretch" Grid.RowSpan="3" HorizontalAlignment ="Left" Width="5"/>
,如下图

<Window x:Class="WPF_Code.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid UseLayoutRounding="True">
<!--Grid.RowDefinitions定义行-->
<Grid.RowDefinitions>
<RowDefinition></RowDefinition><!--一行-->
<RowDefinition ></RowDefinition><!--一行-->
</Grid.RowDefinitions>
<!--Grid.ColumnDefinitions定义列-->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition><!--一列-->
<ColumnDefinition Width="*"></ColumnDefinition><!--一列-->
<ColumnDefinition Width="*"></ColumnDefinition><!--一列-->
</Grid.ColumnDefinitions>
<!--在第二行第一列上加一个按钮-->
<Button Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Grid.ColumnSpan="1">你好</Button>
<!--添加侵害容器-->
<GridSplitter Grid.Column="1" VerticalAlignment="Stretch" Grid.RowSpan="2" HorizontalAlignment ="Left" Width="5"/>
</Grid>
</Window>
共享尺寸组
SharedSizeGroup属性允许相同名的元素具有等大的效果
需要在父容器 设置属性 Grid.IsSharedSizeScope="True"

示例如下:
<Window x:Class="WPF_Code.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<!--定义一个父容器,Grid.IsSharedSizeScope="True"是必须的-->
<Grid ShowGridLines="True" Grid.IsSharedSizeScope="True">
<!--在父容器中定义了两行-->
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<!--在父容器的第一行定义一个子容器-->
<Grid ShowGridLines="True" Name="grid_1" Background="blue" Grid.Row="0">
<Grid.ColumnDefinitions>
<!--第一个子容器的第一列指定了SharedSizeGroup属性-->
<ColumnDefinition Width="50" SharedSizeGroup="A" ></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
</Grid>
<!--在父容器的第二行定义一个子容器-->
<Grid ShowGridLines="True" Name="grid_2" Background="Green" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<!--第二个子容器的第二列指定了SharedSizeGroup属性-->
<ColumnDefinition SharedSizeGroup="A" ></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
</Grid>
</Grid>
</Window>
Canvas面板
Canvas面板是基于坐标的布局容器;Canvas中的子元素的位置是需要显性设定的固定位置,通过Canvas的属性Left/Rigth/Top/Bottom来设置子元素相的位置
下例是一个距离下边界20、左边界20位置的一个按钮

<Window x:Class="WPF_Code.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas>
<Button Canvas.Bottom="20" Canvas.Left="20">Hello</Button>
</Canvas>
</Window>
上例描述的是元素在Canvas容器中的二维坐标,即X-Y坐标,还可以定义元素第三维坐标,即Z坐标f
下例中定义了三个图形,一个圆,三个矩形,它们之间的重叠是按默认的顺序叠加的(未定义过Z坐标的,其Z坐标是0),可以通过设定Canvas.ZIndex属性值或Panel.ZIndex来调整元素的重叠顺序,值越大的越靠前

<Window x:Class="WPF_Code.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas>
<Ellipse Fill="Gainsboro" Canvas.Left="25" Canvas.Top="25" Width="200" Height="200" />
<Rectangle Canvas.ZIndex="3" Fill="LightBlue" Canvas.Left="25" Canvas.Top="25" Width="50" Height="50" />
<Rectangle Panel.ZIndex="2" Fill="LightCoral" Canvas.Left="50" Canvas.Top="50" Width="50" Height="50" />
<Rectangle Panel.ZIndex="1" Fill="LightCyan" Canvas.Left="75" Canvas.Top="75" Width="50" Height="50" />
</Canvas>
</Window>
调整矩形的顺序,如下:

<Window x:Class="WPF_Code.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas>
<Ellipse Fill="Gainsboro" Canvas.Left="25" Canvas.Top="25" Width="200" Height="200" />
<Rectangle Canvas.ZIndex="1" Fill="LightBlue" Canvas.Left="25" Canvas.Top="25" Width="50" Height="50" />
<Rectangle Panel.ZIndex="2" Fill="LightCoral" Canvas.Left="50" Canvas.Top="50" Width="50" Height="50" />
<Rectangle Panel.ZIndex="3" Fill="LightCyan" Canvas.Left="75" Canvas.Top="75" Width="50" Height="50" />
</Canvas>
</Window>
InkCavas元素
InkCavas是一个画图控件,它允许在它之上加载其它控件,比如加载一个按钮;可以通过InkCanvas的Right/Left/Bottom/Top属性来指定元素的位置

<Window x:Class="WPF_Code.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<InkCanvas>
<Button Content="Hello" InkCanvas.Bottom="50" InkCanvas.Left="30" />
</InkCanvas>
</Window>
运行程序后,可发现,当鼠标移动到InkCanvas区域时,鼠标会变成一个黑点,按下鼠标左键移动会画出线条

InkCanvas的EditingMode的属性可根据不同的值,设定图画的不同功能
EditingMode="EraseByPoint"
表示鼠标变成一个可擦除点的工具
EditingMode="EraseByStroke"
表示鼠标变成一个可橡皮擦
EditingMode="GestureOnly"
表示按下鼠标可画线,鼠标松开后所画的线条将消失
EditingMode="Ink"
表示按下鼠标可画出墨线,本属性值是默认的
EditingMode="InkAndGesture"
与Ink功能类似,但同时可以识别用户的手势
EditingMode="None"
表示没有任何反应
EditingMode="Select"
表示可以选择一个元素,可以移动它、删除它,改变形状等
下面通过将这些属性动态的加载到下拉框中,通过选择不同的属性值来体验不同的画图效果

<Window x:Class="WPF_Code.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<InkCanvas Grid.Row="1" Name="incas">
<Button Content="Hello" InkCanvas.Bottom="50" InkCanvas.Left="30" />
</InkCanvas>
<ComboBox Grid.Row="0" Name="cmb_model" SelectionChanged="cmb_model_SelectionChanged" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPF_Code
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
/// <summary>
/// 下拉框选择事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cmb_model_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//将InkCavas的EditingMode属性设置为选择后的属性
//需要将SelectedItem强转成InkCanvasEditingMode类型
this.incas.EditingMode = (InkCanvasEditingMode)this.cmb_model.SelectedItem;
}
/// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Enum.GetValues用于检索指定枚举中常数值的数组
//typeof用于获取类型的 System.Type 对象
foreach (InkCanvasEditingMode item in Enum.GetValues(typeof(InkCanvasEditingMode)))
{
//循环将InkCanvasEditingMode枚举值添加到下拉框中
this.cmb_model.Items.Add(item);
}
//设置默认的选择项为第一项
this.cmb_model.SelectedIndex = 0;
}
}
}
网友评论