71.StackPanel黑白棋盘 WPF例子 C#例子

就是生成黑白棋盘,利用该控件能自动排列的功能。用一个横向的StackPanel嵌套纵向的StackPanel,然后在里面添加设定好长和高的矩形。

因为StackPanel是按照控件的大小展示的。所以如果不设置长和宽。就会显示不出矩形。

<StackPanel Orientation="Horizontal" Margin="0">

<StackPanel Orientation="Vertical" Margin="0">

横向与纵向要设置,才能排列

展示:

XML 复制代码
<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>
相关推荐
weixin_476958274 分钟前
Python 项目中创建虚拟环境(Virtual Environment)
开发语言·python
鲤籽鲲18 分钟前
C# ManualResetEvent 类 使用详解
java·开发语言·c#·多线程
赵璘婳20 分钟前
Perl语言的云计算
开发语言·后端·golang
加油,旭杏42 分钟前
【C++语言】C++入门
开发语言·c++
martian6651 小时前
【Java基础篇】——第4篇:Java常用类库与工具类
java·开发语言
在下陈平安1 小时前
java-LinkedList源码详解
java·开发语言
C66668881 小时前
同步(Synchronous)和异步(Asynchronous)
开发语言·c#
MYX_3092 小时前
第七节 文件与流
开发语言·c++·学习·算法
m0_748248942 小时前
在线影视播放网站PHP电影网站源码自动采集MKCMS升级版米酷模板含WAP手机版附三套模板
android·开发语言·php
~怎么回事啊~3 小时前
chrome-mojo C++ Bindings API
开发语言·c++