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

  • Home
  • SEARCH
  • 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 8917481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:29:34+00:00 2026-06-15T05:29:34+00:00

with the objective to learn C#, XAML and especially MVVM I started to program

  • 0

with the objective to learn C#, XAML and especially MVVM I started to program the Minesweeper game. First version I made was without the MVVM part, where I created and added the buttons with C# code instead of doing it in XAML via the MVVM way. Now I try to apply the MVVM pattern into the game.

I made an own User Control which contains a button representing a minefield part. This control also has a ViewModel and a Model Class to store some state data and handle some commands. In a test project I create 4 of those own User Controls and try to put them in a grid where the buttons forms a square of 2 by 2 buttons. In the code behind, the buttons are put in an ObservableCollection object. I assume that in this object the buttons are listed and indexed like:

Button1
Button2
Button3
Button4

but in the presentation grid I want the buttons to be shown like

Button1 | Button2
--------+--------
Button3 | Button4

The question is: how do I do that dynamicly? Cause in my test project I test with 4 buttons, but In the project I want to use this, the amount of buttons can be different depending on the difficulty of the game the player has chosen.

And a second question is how I can figure out what the neigbhours are of a button. So if the grid is 4 by 5 containing 20 buttons. And for example I pick button 8 which has as neighbour buttons number 2, 3, 4, 7, 9, 12, 13 and 14. How can I reach those neighbour buttons when the buttons are listed?

I hope my questions are clear enough.

Thank you in advance!

  • 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-06-15T05:29:35+00:00Added an answer on June 15, 2026 at 5:29 am

    You can display your collection using an ItemsControl that has it’s ItemsPanel set to a Grid or UniformGrid. I have some ItemsControl examples here that may help you, as I don’t find MDSN’s examples very helpful.

    A UniformGrid would be easiest if you can bind your Rows and Columns properties to a property in your ViewModel, however that requires all your cells be the same size, and I can’t remember if the properties Rows and Columns are DependencyProperties that participate in the binding system or not.

    <ItemsControl ItemsSource="{Binding MyCollection}">
        <!-- ItemsPanelTemplate -->
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Columns="{Binding ColumnCount}" 
                             Rows="{Binding RowCount}" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
    

    If this doesn’t work for you, you can use a Grid as the ItemsPanel and set the Grid.Row and Grid.Column bindings in the ItemContainerStyle.

    This will require you have properties on each of your cell objects in the ObservableCollection to say what Row/Column that cell is in, however I suspect you’ll need these anyways to determine things like adjacent cells in your click command.

    In addition, there’s no built-in way to bind the number of Rows/Columns in your Grid, so I tend to use some custom attached properties that will dynamically build the Grid’s RowDefinitions and ColumnDefinitions based on a bound value.

    So your end result if you’re using a Grid would probably look something like this:

    <ItemsControl ItemsSource="{Binding Cells}">
        <!-- ItemsPanelTemplate -->
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <!-- This is assuming you use the attached properties linked above -->
                <Grid local:GridHelpers.RowCount="{Binding Height}"
                      local:GridHelpers.ColumnCount="{Binding Width}" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    
        <!-- ItemContainerStyle -->
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Grid.Column" 
                        Value="{Binding ColumnIndex}" />
                <Setter Property="Grid.Row" 
                        Value="{Binding RowIndex}" />
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>
    

    With a Model/ViewModel looking something like this

    public class GameViewModel
    {
        // These should be full properties w/ property change notification
        // but leaving that out for simplicity right now
        public int Height;
        public int Width;
        public ObservableCollection<CellModel> Cells;
    }
    
    public class CellModel
    {
        // Same thing, full properties w/ property change notification
        public int ColumnIndex;
        public int RowIndex;
        public bool IsVisible;
        public bool IsMine;
        public int NumberAdjacentMines;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently started to learn Objective-C and write my tests using OCUnit that
First off I am still new to objective-c and still trying to learn as
I just started to learn Objective c. Now i want a under title (small
I'm coming from C# development and just started to learn Objective-C and Xcode 4.
I'm reading a book to learn Objective-C and this program is suppose to show
I'm trying to learn Objective-C and my program (creating a calculator) gets linker or
I just started to learn objective-c and one thing is not clear for me
OK, first and foremost, I am using GNUStep as a way to learn Objective-C,
I am trying to learn objective c on windows. My program compiles with warnings
I'm trying to learn objective-c and get some problem. I've created class Creature and

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.