Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chessboard in WPF

Tags:

wpf

chess

For years I've developed with Winforms, now I want to switch to WPF and make a chessboard. Unfortunately I have no idea where to start. Using WPF makes me very unsure, I'm feeling like a noob again. Can someone outline a basic design? I guess I would start with a 8x8 Grid and use rectangles for the squares, images for pieces. And then? Am I missing something?

Edit: It's just about the user interface; what goes on behind the scenes is no problem.

like image 804
CaptainProton Avatar asked Dec 27 '09 16:12

CaptainProton


2 Answers

An alternative to the standard grid, is the use of a UniformGrid (msdn link).

It's probably more suited to this (in my opinion) as it will ALWAYS give you the same size cells.

used ala:

<UniformgGrid Columns="8" Rows="8">
    <Control1/>
    <Control2/>
    <Control3/>
</UniformGrid>

any of these answers will give you the results you desire.

like image 91
Alastair Pitts Avatar answered Sep 19 '22 18:09

Alastair Pitts


Chess seems like a good fit for WPF's MVVM code pattern.

The Model will be the logic for the game of chess, sounds like you have that under control. The View will the the WPF look of the game, and the ViewModel will the representation of the game, in which the View can databind to.

For the view, an ItemsControl using a UniformGrid will work for a 2D representation of the game.

Here's a start (unchecked)

<ItemsControl ItemsSource="{Binding TheGame}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="8" Rows="8" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ContentControl Background="{Binding SquareColor}">
                <Path Data="{Binding PieceShape}" Fill="{Binding PieceColor}" />
            </ContentControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

For the above to work, your ViewModel will need to have a ObservableCollection<ChessGridItem> and ChessGridItem should be a DependencyObject exposing DependencyProperties for SquareColor, PieceColor and PieceShape

like image 26
Scott Weinstein Avatar answered Sep 21 '22 18:09

Scott Weinstein