WPF界面布局的10个例子
1.使用按钮填充父容器,类似于Winform中将按钮的Dock设置为Fill
XAML代码如下:
<Window x:Class="WpfLayoutDemo.Window01" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="按钮填充整个工作区" Height="300" Width="300"> <Grid Name="BaseGrid"> <Button Content="Button" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="{Binding Path=ActualWidth,ElementName=BaseGrid}" Height="{Binding Path=ActualHeight,ElementName=BaseGrid}"/> </Grid> </Window>
2.使用Dockpanel进行布局,容器分上、下、左、右、中央五个部分
XAML代码如下:
<Window x:Class="WpfLayoutDemo.Window02" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="DockPanel布局测试" Height="300" Width="300"> <DockPanel> <Button Content="Button" DockPanel.Dock="Top" Height="80"/> <Button Content="Button" DockPanel.Dock="Bottom" Height="80"/> <Button Content="Button" DockPanel.Dock="Left"/> <Button Content="Button" DockPanel.Dock="Right"/> <Button Content="Button" /> </DockPanel> </Window>
3.使用Dockpanel进行布局,调整Dock声明的先后顺序可调整DockPanel内元素的结构
XAML代码如下:
<Window x:Class="WpfLayoutDemo.Window03" xmlns="http://schemas.microso