WPF棋盘-如何在XAML中绘制

WPF棋盘-如何在XAML中绘制,wpf,grid,tiles,Wpf,Grid,Tiles,我需要用WPF画一个棋盘。我是WPF新手,用XAML编写一些工作代码对我来说相当复杂 到目前为止,我读到我可能应该使用TileBrush,但我真的不知道如何使用它。我还读到一些人使用UniformGrid绘制图板,但是我不知道如何以正确的顺序填充网格的单元格。此外,我使用canvas来执行所有操作,所以我不知道是否可以在canvas中使用uniformgrid 我还希望董事会调整大小,如果我改变了窗口的尺寸 可以这样做吗?这是通过代码隐藏完成的 如果需要在XAML中使用UniformGrid

我需要用WPF画一个棋盘。我是WPF新手,用XAML编写一些工作代码对我来说相当复杂

到目前为止,我读到我可能应该使用TileBrush,但我真的不知道如何使用它。我还读到一些人使用UniformGrid绘制图板,但是我不知道如何以正确的顺序填充网格的单元格。此外,我使用canvas来执行所有操作,所以我不知道是否可以在canvas中使用uniformgrid

我还希望董事会调整大小,如果我改变了窗口的尺寸


可以这样做吗?

这是通过代码隐藏完成的

如果需要在XAML中使用UniformGrid

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
x:Class="WpfApplication9.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="600">
<Window.Resources>
<Color x:Key="BlackRes">Black</Color>
</Window.Resources>

<Grid x:Name="LayoutRoot">
    <UniformGrid Margin="29,29.5,23,32.5" Height="500">
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Fill="White" Stroke="Black"/>
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BlackRes}"/>
            </Rectangle.Fill>
        </Rectangle>
    </UniformGrid>
</Grid>

黑色