tags:

views:

87

answers:

1

I need to draw a chessboard in WPF. I'm new in WPF and writing some working code in XAML is quite complicated for me.

So far I read that I should probably use a TileBrush however I don't really know how to use it. I also read that some people draw boards using UniformGrid, however I don't know how to fill in the grid's cells in the proper order. What is more I use canvas to do all my operations, so I don't know if I can use uniformgrid in canvas.

I also would like the board to resize if I change the dimensions of the window.

Is it possible to do that?

+1  A: 

This done by code behind

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

and if you want in XAML use 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>

Kishore Kumar
i added XAML code for that using UniformGrid
Kishore Kumar