就是生成黑白棋盘,利用该控件能自动排列的功能。用一个横向的StackPanel嵌套纵向的StackPanel,然后在里面添加设定好长和高的矩形。
因为StackPanel是按照控件的大小展示的。所以如果不设置长和宽。就会显示不出矩形。
<StackPanel Orientation="Horizontal" Margin="0">
<StackPanel Orientation="Vertical" Margin="0">
横向与纵向要设置,才能排列
展示:
<Window x:Class="StackPanel.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:StackPanel"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Border BorderThickness="40" BorderBrush="Brown">
<StackPanel Orientation="Horizontal" Margin="0">
<StackPanel Orientation="Vertical" Margin="0">
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0">
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0">
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0">
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0">
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0">
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0">
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0">
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0">
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0">
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0">
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0">
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0">
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0">
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0">
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0">
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
<Rectangle Fill="White" Width="50" Height="50"/>
<Rectangle Fill="Black" Width="50" Height="50"/>
</StackPanel>
</StackPanel>
</Border>
</Grid>
</Window>