WPF 布局UniformGrid使用案例
一、UniformGrid
UniformGrid 是 WPF 中的一种特殊布局控件,所有子元素会被均匀分配到相同大小的单元格中,且每个单元格的尺寸一致。它适用于需要等分空间的场景(如棋盘、按钮矩阵、图片缩略图等)
关键属性详解
Rows 和 Columns
作用:手动指定行数和列数。若未设置,根据子元素数量和容器尺寸自动计算。
自动计算规则:
优先填满行(类似 Horizontal 方向的 WrapPanel)。
<Window x:Class="WpfAppNet8.Panel.UniformGrid1" 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:WpfAppNet8.Panel" mc:Ignorable="d" Title="UniformGrid1" Height="450" Width="800"> <UniformGrid > <Button>测试001</Button> <Button>测试002</Button> <Button>测试003</Button> <Button>测试004</Button> <Button>测试005</Button> </UniformGrid> </Window>

二、案例2
<Window x:Class="WpfAppNet8.Panel.UniformGrid2" 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:WpfAppNet8.Panel" mc:Ignorable="d" Title="UniformGrid2" Height="450" Width="800"> <UniformGrid Rows="2" Columns="3"> <Button>测试001</Button> <Button>测试002</Button> </UniformGrid> </Window>

更多:
浙公网安备 33010602011771号