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>
相关推荐
m0_555762909 分钟前
qt style-sheet样式不起作用问答
开发语言·qt
啊吧怪不啊吧12 分钟前
C++相关基础概念之入门讲解(上)
c语言·开发语言·c++
破刺不会编程12 分钟前
Linux中的权限
linux·运维·服务器·开发语言
灏瀚星空15 分钟前
Python JSON模块详解:从入门到高级应用
开发语言·经验分享·笔记·python·json
azaz_plus19 分钟前
C++ priority_queue 堆
开发语言·c++·stl··priority_queue
上官美丽20 分钟前
单一责任原则在Java设计模式中的深度解析
java·开发语言·设计模式
橙序研工坊23 分钟前
Java基础语法练习42(基本绘图-基本的事件处理机制-小坦克的绘制-键盘控制坦克移动)
java·开发语言
江沉晚呤时36 分钟前
Markdig:强大的 .NET Markdown 解析器详解
c#·asp.net·.netcore·net
爱敲代码的TOM1 小时前
基于自定义线程池手写一个异步任务管理器
java·开发语言
蜡笔小新星1 小时前
DevOps实践:持续集成与持续部署完全指南
运维·开发语言·经验分享·ci/cd·devops