DockPanel是WPF中最常用的布局控件之一,它允许子元素沿着面板的边缘停靠。与WinForm中的Dock属性类似,但功能更加强大和灵活。DockPanel可以让子元素依次停靠在上、下、左、右四个方向,最后一个子元素默认会填充剩余空间。
XML<Window x:Class="AppDockPanel.MainWindow"
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:AppDockPanel"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<DockPanel>
<!-- 顶部按钮 -->
<Button DockPanel.Dock="Top" Height="30">顶部按钮</Button>
<!-- 底部状态栏 -->
<Button DockPanel.Dock="Bottom" Height="30">底部按钮</Button>
<!-- 左侧导航栏 -->
<Button DockPanel.Dock="Left" Width="100">左侧按钮</Button>
<!-- 右侧工具栏 -->
<Button DockPanel.Dock="Right" Width="100">右侧按钮</Button>
<!-- 中间内容区域(最后一个元素会自动填充剩余空间) -->
<Button>中间内容</Button>
</DockPanel>
</Window>

StackPanel是WPF中最简单和常用的布局控件之一,它可以将子元素按照水平或垂直方向依次排列。对于从WinForm转型到WPF的开发者来说,理解StackPanel的使用对掌握WPF布局系统至关重要。
XML<Window x:Class="AppStackPanel.MainWindow"
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:AppStackPanel"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<!-- 垂直方向的StackPanel -->
<StackPanel Margin="10">
<Button Content="按钮1" Height="30" Margin="0,0,0,5"/>
<Button Content="按钮2" Height="30" Margin="0,0,0,5"/>
<Button Content="按钮3" Height="30"/>
</StackPanel>
</Window>

在Winform中可以用Table控件实现,但说实话在Winform中的Table控件做的实在不好用,不少功能的逻辑实在不敢恭维。Grid(网格)是WPF中最灵活和最常用的布局控件之一。它允许我们将界面划分为行和列,形成类似表格的结构,可以精确控制元素的位置和大小。Grid非常适合创建复杂的用户界面布局。这个可以说是WPF功能最全的布局控件了,这个比Winform中的Table好用太多了。。。
RowDefinitions: 定义行ColumnDefinitions: 定义列Grid.Row: 设置元素所在行Grid.Column: 设置元素所在列Grid.RowSpan: 设置元素跨越的行数Grid.ColumnSpan: 设置元素跨越的列数Grid支持三种尺寸单位:
Width="100")Auto)*)C#public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Panel作为容器
Panel panel = new Panel();
panel.Location = new Point(10, 10);
panel.Size = new Size(200, 100);
panel.BackColor=Color.Red;
// GroupBox作为容器
GroupBox groupBox = new GroupBox();
groupBox.Location = new Point(10, 120);
groupBox.Size = new Size(200, 100);
groupBox.Text = "分组";
this.Controls.Add(panel);
this.Controls.Add(groupBox);
}
}

复制构造函数是C#中的一种特殊构造函数,用于创建一个对象的精确副本。它接受一个与当前类相同类型的对象作为参数,并复制该对象的属性和字段值到新创建的对象中。
复制构造函数的主要目的是:
复制构造函数的基本语法如下:
C#public class ClassName
{
public ClassName(ClassName other)
{
// 复制 other 对象的属性到新对象
}
}