Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 697023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:07:31+00:00 2026-05-14T03:07:31+00:00

How can I create board for simple game, looks like for chess,but user could

  • 0

How can I create board for simple game, looks like for chess,but user could dynamically change number of column and rows? In cells I could insert symbol of pawn, like small image or just ellipse or rectangle with fill. This board should have possibility add and remove pawn from cells and move pown from one cell to another.

My first idea was Grid. I do it in code behind, but it’s hurt to implement events or everything in runtime create board :/

        int size = 12;
        Grid board = new Grid();
        board.ShowGridLines = true;
        for (int i = 0; i < size;i++ )
        {
            board.ColumnDefinitions.Add(new ColumnDefinition());
            board.RowDefinitions.Add(new RowDefinition());
        }

        //komputer
        Rectangle ai = new  Rectangle();
        ai.Height = 20;
        ai.Width = 20;
        ai.AllowDrop = true;
        ai.Fill = Brushes.Orange;
        Grid.SetRow(ai, 0);
        Grid.SetColumn(ai,0);
        //człowiek
        Rectangle hum = new Rectangle();
        hum.Height = 20;
        hum.Width = 20;
        hum.AllowDrop = true;
        hum.Fill = Brushes.Green;
        Grid.SetRow(hum,size);
        Grid.SetColumn(hum,size);
        board.Children.Add(ai);
        board.Children.Add(hum);
        this.Content = board;
  • It’s a way to do this dynamically col and row change in XAML ?
  • It’s better way to implement that board create and implement events on move pawn from one cell to another ?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:07:31+00:00Added an answer on May 14, 2026 at 3:07 am

    You’ll still have to use code-behind to change the RowDefinitions and ColumnDefinitions properties of the Grid in this example, since they’re not dependency properties. But all the rest of the logic can be handled in the view model class.

    The XAML:

    <Window x:Class="GameBoard.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:GameBoard="clr-namespace:GameBoard"
            Title="Window1"
            SizeToContent="WidthAndHeight">
        <Grid Margin="50">
            <Grid.Resources>
    
                <!-- This template presents the Piece object.  Note that you can't set
                     the Grid.Row and Grid.Column properties on this Rectangle - well,
                     you *can*, but the Grid won't see them.  See the Style below.  -->
                <DataTemplate DataType="{x:Type GameBoard:Piece}">
                    <Rectangle Fill="{Binding Fill}"
                               Width="50"
                               Height="50" />
                </DataTemplate>
    
                <!-- When the ItemsControl creates its items, it wraps each item in a
                     ContentPresenter.  You have to set Grid.Row and Grid.Column
                     on this ContentPresenter in order for the Grid to see them. -->
                <Style TargetType="{x:Type ContentPresenter}">
                    <Setter Property="Grid.Row"
                            Value="{Binding Row}" />
                    <Setter Property="Grid.Column"
                            Value="{Binding Column}" />
                </Style>
    
            </Grid.Resources>
            <Border BorderBrush="Black"
                    BorderThickness="1">
                <ItemsControl x:Name="Board"
                              ItemsSource="{Binding}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="50" />
                                    <RowDefinition Height="50" />
                                    <RowDefinition Height="50" />
                                    <RowDefinition Height="50" />
                                    <RowDefinition Height="50" />
                                    <RowDefinition Height="50" />
                                    <RowDefinition Height="50" />
                                    <RowDefinition Height="50" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition Width="50" />
                                </Grid.ColumnDefinitions>
                            </Grid>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
            </Border>
        </Grid>
    </Window>
    

    The Piece class – obviously you’ll need to implement INotifyPropertyChanged on the Row and Column properties to handle moving pieces about.

    public class Piece
    {
        public int Column { get; set; }
        public Brush Fill { get; set; }
        public int Row { get; set; }
    }
    

    Populating the board:

        public Window1()
        {
            InitializeComponent();
    
            ObservableCollection<Piece> pieces = new ObservableCollection<Piece>();
            pieces.Add(
                new Piece {Row = 0, Column = 0, Fill = new SolidColorBrush(Colors.BlanchedAlmond)});
            pieces.Add(
                new Piece {Row = 7, Column = 7, Fill = new SolidColorBrush(Colors.RosyBrown)});
            pieces.Add(
                new Piece { Row = 3, Column = 4, Fill = new SolidColorBrush(Colors.BlueViolet) });
            pieces.Add(
                new Piece { Row = 5, Column = 4, Fill = new SolidColorBrush(Colors.Orange) });
    
            Board.DataContext = pieces;
        }
    

    I’ve used an ItemsControl to contain the pieces in this example. You could use a ListBox instead – that’s kind of nice because it gives you item selection for free. Note that if you do this you’ll have to change the Style‘s TargetType to ListBoxItem, since that’s what the ListBox wraps its item elements in instead of ContentPresenter.

    Edit:

    I wrote this answer quite a while ago, and it’s got a problem.

    Assigning the Grid.Row and Grid.Column properties using a style that’s applied to the item container generated by the grid is right. Figuring out that the item container is a ContentPresenter and creating a default style for that type is not. (It’ll work reliably in this case, but there are lots of cases where it won’t.)

    You should still create a style, but it should be assigned to the ItemsControl‘s ItemContainerStyle. This style’s automatically applied to whatever container element the control generates for its items – so if the ItemsControl you’re using is a ListBox, it will apply it to the ListBoxItem, and if it’s a TabControl, it’ll get applied to the TabItem, and so on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Postgresql you can create additional Aggregate Functions with CREATE AGGREGATE name(...); But this
Is there anyway I can create a not in clause like I would have
I am working on a generic game engine for simple board games and such.
I'm working on a browser-game and I can't help but wonder about what's the
I'd like to create a component, which consist from a board and its surrounding
I need help in designing a Chess game. I've already started but haven't got
I am trying to create a board game using html/css/javascript to be played on
I can create a menu item in the Windows Explorer context menu by adding
I can create the following and reference it using area[0].states[0] area[0].cities[0] var area =
I can create a literal long by appending an L to the value; why

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.