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>
相关推荐
敖云岚7 分钟前
【云原生技术】容器技术的发展史
开发语言·云原生·perl
忧郁的蛋~23 分钟前
JavaScript性能优化的12种方式
开发语言·javascript·性能优化
人工智能研究所25 分钟前
使用OpenCV与Python编写自己的俄罗斯方块小游戏
开发语言·python·opencv
DDD小小小宇宙25 分钟前
python列表基础知识
开发语言·windows·python
海盗强27 分钟前
prototype和proto的区别
开发语言·javascript·原型模式
哥谭居民00011 小时前
mybatis注册一个自定义拦截器,拦截器用于自动填充字段
java·开发语言·jvm·mybatis
钟离墨笺1 小时前
【c++】【智能指针】什么情况下不适合智能指针
开发语言·c++
moz与京1 小时前
【记】如何理解kotlin中的委托属性?
android·开发语言·kotlin
左少华1 小时前
Kotlin-inline函数特效
android·开发语言·kotlin
懒大王爱吃狼1 小时前
Python + Qt Designer构建多界面GUI应用程序:Python如何调用多个界面文件
开发语言·数据库·python·qt·mysql·python基础·命令模式