WPF棋盘 - 如何在XAML中绘制

时间:2010-08-14 10:37:32

标签: wpf grid tiles

我需要在WPF中画一个棋盘。我是WPF的新手,在XAML中编写一些工作代码对我来说非常复杂。

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

如果我更改窗口的尺寸,我也希望电路板调整大小。

有可能吗?

1 个答案:

答案 0 :(得分:2)

这是由

背后的代码完成的

http://www.c-sharpcorner.com/UploadFile/mahesh/391/

如果你想在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>