赞
踩
// StackPanel 里添加了4个按钮,默认布局,Orientation 默认垂直排列
<Window x:Class="WpfFirstApp.StackPanelWindow"
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:WpfFirstApp"
mc:Ignorable="d"
Title="StackPanelWindow" Height="450" Width="800">
<StackPanel>
<Button Content="按钮1"></Button>
<Button Content="按钮2"></Button>
<Button Content="按钮3"></Button>
<Button Content="按钮4"></Button>
</StackPanel>
</Window>
// 设置 Orientation 属性(Horizontal)
<Window x:Class="WpfFirstApp.StackPanelWindow"
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:WpfFirstApp"
mc:Ignorable="d"
Title="StackPanelWindow" Height="450" Width="800">
<StackPanel Orientation="Horizontal">
<Button Content="按钮1"></Button>
<Button Content="按钮2"></Button>
<Button Content="按钮3"></Button>
<Button Content="按钮4"></Button>
</StackPanel>
</Window>
// 使用C# 代码直接实现
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.Shapes;
namespace WpfFirstApp
{
/// <summary>
/// StackPanelWindow.xaml 的交互逻辑
/// </summary>
public partial class StackPanelWindow : Window
{
public StackPanelWindow()
{
InitializeComponent();
this.Title = "StackPanel面板";
// StackPanel 默认排列
StackPanel stackPanel = new StackPanel();
this.Content = stackPanel;
stackPanel.Children.Add(new Button()
{
Content="按钮1"
});
stackPanel.Children.Add(new Button()
{
Content = "按钮2"
});
stackPanel.Children.Add(new Button()
{
Content = "按钮3"
});
stackPanel.Children.Add(new Button()
{
Content = "按钮4"
});
}
}
}
// xaml中直接设置属性Margin="30" 当值设置一个值时,wpf 会默认填充上下左右边距为30
// 或 Margin="30 30 30 30 " 设置 左 上 右 下边距都为30
<Window x:Class="WpfFirstApp.StackPanelWindow"
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:WpfFirstApp"
mc:Ignorable="d"
Title="StackPanelWindow" Height="450" Width="800">
<StackPanel Orientation="Vertical" Margin="30">
<Button Content="按钮1"></Button>
<Button Content="按钮2"></Button>
<Button Content="按钮3"></Button>
<Button Content="按钮4"></Button>
</StackPanel>
</Window>
// 使用c# 代码实现设置外边距 30 ,stackPanel.Margin = new Thickness(30)
// 或 stackPanel.Margin = new Thickness(30,30,30,30); 左上右下 设置为30
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.Shapes;
namespace WpfFirstApp
{
/// <summary>
/// StackPanelWindow.xaml 的交互逻辑
/// </summary>
public partial class StackPanelWindow : Window
{
public StackPanelWindow()
{
InitializeComponent();
this.Title = "StackPanel面板";
StackPanel stackPanel = new StackPanel();
this.Content = stackPanel;
// 设置为横排
stackPanel.Orientation = Orientation.Vertical;
// 设置外边距 30
stackPanel.Margin = new Thickness(30);
//或 设置外边距
//stackPanel.Margin = new Thickness(30,30,30,30);
stackPanel.Children.Add(new Button()
{
Content = "按钮1"
});
stackPanel.Children.Add(new Button()
{
Content = "按钮2"
});
stackPanel.Children.Add(new Button()
{
Content = "按钮3"
});
stackPanel.Children.Add(new Button()
{
Content = "按钮4"
});
}
}
}
// 设置Orientation="Vertical" VerticalAlignment="Center"
<Window x:Class="WpfFirstApp.StackPanelWindow"
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:WpfFirstApp"
mc:Ignorable="d"
Title="StackPanelWindow" Height="450" Width="800">
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button Content="按钮1"></Button>
<Button Content="按钮2"></Button>
<Button Content="按钮3"></Button>
<Button Content="按钮4"></Button>
</StackPanel>
</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.Shapes;
namespace WpfFirstApp
{
/// <summary>
/// StackPanelWindow.xaml 的交互逻辑
/// </summary>
public partial class StackPanelWindow : Window
{
public StackPanelWindow()
{
InitializeComponent();
this.Title = "StackPanel面板";
StackPanel stackPanel = new StackPanel();
this.Content = stackPanel;
// 设置为竖排
stackPanel.Orientation = Orientation.Vertical;
stackPanel.VerticalAlignment = VerticalAlignment.Center;
stackPanel.Children.Add(new Button()
{
Content = "按钮1"
});
stackPanel.Children.Add(new Button()
{
Content = "按钮2"
});
stackPanel.Children.Add(new Button()
{
Content = "按钮3"
});
stackPanel.Children.Add(new Button()
{
Content = "按钮4"
});
}
}
}
用于将子元素排列在一行或一列中,当没有足够的空间时,它会将元素移动到下一行或列。与 StackPanel 不同,WrapPanel 会自动处理元素的对齐和换行。它也可以使用 Orientation 属性更改排列方向。Orientation 属性和StackPanel 一样,唯一的区别就是 排列 默认不同,WrapPanel 排列方式 横排(Horizontal,默认)和竖排(Vertical)
例如: 横排(Horizontal,默认)
// 默认排列方式
<Window x:Class="WpfFirstApp.WrapPanelWindow"
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:WpfFirstApp"
mc:Ignorable="d"
Title="WrapPanelWindow" Height="450" Width="800">
<WrapPanel>
<Button Content="按钮1" Height="80" Width="90"></Button>
<Button Content="按钮2" Height="80" Width="90"></Button>
<Button Content="按钮3" Height="80" Width="90"></Button>
<Button Content="按钮4" Height="80" Width="90"></Button>
<Button Content="按钮5" Height="80" Width="90"></Button>
<Button Content="按钮6" Height="80" Width="90"></Button>
<Button Content="按钮7" Height="80" Width="90"></Button>
<Button Content="按钮8" Height="80" Width="90"></Button>
<Button Content="按钮9" Height="80" Width="90"></Button>
<Button Content="按钮10" Height="80" Width="90"></Button>
</WrapPanel>
</Window>
// 设置 Orientation="Vertical" 属性
<Window x:Class="WpfFirstApp.WrapPanelWindow"
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:WpfFirstApp"
mc:Ignorable="d"
Title="WrapPanelWindow" Height="450" Width="800">
<WrapPanel Orientation="Vertical">
<Button Content="按钮1" Height="80" Width="90"></Button>
<Button Content="按钮2" Height="80" Width="90"></Button>
<Button Content="按钮3" Height="80" Width="90"></Button>
<Button Content="按钮4" Height="80" Width="90"></Button>
<Button Content="按钮5" Height="80" Width="90"></Button>
<Button Content="按钮6" Height="80" Width="90"></Button>
<Button Content="按钮7" Height="80" Width="90"></Button>
<Button Content="按钮8" Height="80" Width="90"></Button>
<Button Content="按钮9" Height="80" Width="90"></Button>
<Button Content="按钮10" Height="80" Width="90"></Button>
</WrapPanel>
</Window>
// 当只有个按钮时,不控制宽高度时,默认填充所有的地方
// 增加一个按钮,第一个默认高度,并设置宽度为70,第二个高度会填充满然后看效果
// 可以依次添加停靠位置,并查看对应的效果
<Window x:Class="WpfFirstApp.DockPanelWindow"
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:WpfFirstApp"
mc:Ignorable="d"
Title="DockPanel停靠面板" Height="450" Width="800">
<DockPanel>
<!--设置宽度为70,停靠方向为上-->
<Button Content="按钮靠上" Width="70" DockPanel.Dock="Top"></Button>
<!--增加一个按钮设置宽度为70,停靠方向为上,并设置右对齐-->
<Button Content="按钮靠上右" Width="70" HorizontalAlignment="Right" DockPanel.Dock="Top"></Button>
<!--第三个按钮设置停靠上,并设置外边距10-->
<Button Content="按钮靠上" Margin="10" DockPanel.Dock="Top"></Button>
<!--第四个按钮设置靠左-->
<Button Content="按钮靠左" DockPanel.Dock="Left" ></Button>
<!--第五个按钮设置靠右-->
<Button Content="按钮靠右" DockPanel.Dock="Right"></Button>
<!--第六个按钮设置靠下-->
<Button Content="按钮靠下" DockPanel.Dock="Bottom"></Button>
<!--第七个按钮默认未设置停靠-->
<Button Content="默认未设置停靠"></Button>
</DockPanel>
</Window>
// Grid定义两行两列布局
<Window x:Class="WpfFirstApp.GridWindow"
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:WpfFirstApp"
mc:Ignorable="d"
Title="Grid 面板" Height="450" Width="800">
<Grid>
<!--定义两行,两列-->
<Grid.RowDefinitions>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition Height="200*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<!--设置第一行第一列,并设置为上对齐,为了看效果设置为高度的一半30-->
<Button Grid.Column="0" Grid.Row="0" Height="30" Content="第一行第一列上对齐" VerticalAlignment="Top"></Button>
<!--设置第一行第二列,并设置为下对齐,为了看效果设置为高度的一半30-->
<Button Grid.Column="1" Grid.Row="0" Height="30" Content="第一行第二列下对齐" VerticalAlignment="Bottom"></Button>
<!--演示Grid内容为层叠,再增加一个按钮,会盖住上一个第二列按钮-->
<Button Grid.Column="1" Grid.Row="0" Content="第一行第二列不设置高度和对齐"></Button>
<!--设置第二行-->
<Button Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Content="第二行并合并两列"></Button>
</Grid>
</Window>
// 定义 3*3 的表格,其中放置分割线的列和行的宽度和高度设置为 Auto
<Window x:Class="WpfFirstApp.GridSplitWindow"
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:WpfFirstApp"
mc:Ignorable="d"
Title="GridSplit 分割" Height="450" Width="800">
<Grid>
<!--定义三行三列-->
<Grid.RowDefinitions>
<RowDefinition Height="90*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="190*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="170*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<!--这里可以增加Grid列内容,不添加Button,则只会显示GridSplitter分割线-->
<!--第一列三行左边,并设置外边距3-->
<Button Content="第一列三行左边" Margin="3" Grid.Row="0" Grid.Column="0" Grid.RowSpan="3" />
<Button Content="第一行第二列上边" Margin="3" Grid.Row="0" Grid.Column="2" />
<Button Content="第三行第二列下边" Margin="3" Grid.Row="2" Grid.Column="2" />
<!--添加GridSplitter 分割-->
<!--设置在水平和垂直方向上都拉伸以填充整个空间,设置为第一列。并三行合并-->
<GridSplitter Width="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0" Grid.Column="1" Grid.RowSpan="3"></GridSplitter>
<!--设置在水平和垂直方向上都拉伸以填充整个空间,设置为第二行,为两列-->
<GridSplitter Height="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Grid.Column="2"></GridSplitter>
</Grid>
</Window>
// UniformGrid添加7个按钮它会自动排列
<Window x:Class="WpfFirstApp.UniformGridWindow"
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:WpfFirstApp"
mc:Ignorable="d"
Title="UniformGridWindow" Height="450" Width="800">
<UniformGrid>
<Button Content="按钮1"></Button>
<Button Content="按钮2"></Button>
<Button Content="按钮3"></Button>
<Button Content="按钮4"></Button>
<Button Content="按钮5"></Button>
<Button Content="按钮6"></Button>
<Button Content="按钮7"></Button>
</UniformGrid>
</Window>
公众号“点滴分享技术猿”
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。