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>
相关推荐
Thomas_YXQ8 分钟前
Unity3D ILRuntime与Scripting Backend整合指南
服务器·开发语言·unity·unity3d
Chandler2412 分钟前
Go:反射
开发语言·后端·golang
盒子691019 分钟前
go for 闭环问题【踩坑记录】
开发语言·后端·golang
加点油。。。。21 分钟前
C语言高频面试题——strcpy与memcpy区别
c语言·开发语言
浅陌sss33 分钟前
设计模式 --- 装饰器模式
设计模式·c#
拓端研究室TRL1 小时前
Python+AI提示词比特币数据预测:Logistic逻辑回归、SVC及XGB特征工程优化实践
开发语言·人工智能·python·算法·逻辑回归
du fei1 小时前
C# 单例模式
java·单例模式·c#
weixin_307779131 小时前
实现AWS Step Function安全地请求企业内部API返回数据
开发语言·python·云计算·aws
爱吃巧克力的程序媛1 小时前
C# 的 字符串插值($) 和 逐字字符串(@) 功能
c#
刘 大 望1 小时前
Java写数据结构:栈
java·开发语言·数据结构