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 8506715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:40:01+00:00 2026-06-11T02:40:01+00:00

I’m working on making my first game using C# and XAML for Windows 8.

  • 0

I’m working on making my first game using C# and XAML for Windows 8. I’m still learning the core concepts and best practices, and MVVM has been a hurdle. I’ll attempt to ask the question in two parts.

Background

The game I’m making is Sudoku. Sudoku has a board that contains a 9×9 grid of tiles. I have three models – Game, Board, and Tile. When a Game is created, it automatically creates a Board, and when the Board is created, it creates 81 (9×9) Tiles.

1. With a hierarchy of views, how are corresponding view models created?

To match the hierarchy of models, I would like to have a hierarchy of views (GameView contains a BoardView which contains 81 TileViews). In XAML, it’s pretty easy to create this hierarchy of views with user controls, but I don’t understand how the view models get created.

In the examples I’ve seen, the data context of a user control is often set to the view model (using the ViewModelLocator as a source) which creates a fresh instance of the view model. This seems to work well if you have a flat view, but also seems like it gets messy when you have a hierarchy. Does the GameView create a GameViewModel and leave it up to its BoardView child to create a BoardViewModel? If so, how does the GameViewModel communicate with the BoardViewModel? Can the BoardViewModel communicate back up the hierarchy to the GameViewModel?

2. How does a view model get model data?

In iOS, I would start by using a service to fetch a Game model that was pre-populated with data. I would then create a GameViewController view controller (which was in charge of creating the view) and pass the Game to it. In MVVM, I see the value in having a view be in charge of creating its own view model (ideally using a ViewModelLocator), but I don’t understand how that view model gets the model.

In all of the examples I’ve found online, the view model uses some service to fetch its own data. But I haven’t come across any example that accepts constructor params or params passed from a higher level of navigation. How is this done?

I don’t want to use an application resource or some other kind of singleton storage method for my model because, not that I do, but what if I wanted to display multiple puzzles on the screen at once? Each GameView should contain its own Game.

Not only does the GameViewModel need a reference to the Game model, but the BoardViewModel that was created somehow (see question 1) needs a reference to the Board model that belongs to the Game model. The same goes for all the Tiles. How is all this information passed down the chain? Can I do this much heavy lifting entirely within XAML, or am I going to have to do some sort of binding or other initialization in code?

Phew!

I appreciate any advice you can give, even if it’s not a full answer. I’m also keen to find any examples of MVVM projects that share similar challenges to my own. Thanks a ton!

  • 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-11T02:40:02+00:00Added an answer on June 11, 2026 at 2:40 am

    I would start by creating a class to begin the application with. Typically I call that class something like ApplicationViewModel or ShellViewModel, even though technically it can abide by different rules than what I would typically use for a ViewModel

    This class gets instantiated at startup, and is the DataContext for the ShellView or ApplicationView

    // App.xaml.cs
    private void OnStartup(object sender, StartupEventArgs e)
    {
        var shellVM = new ShellViewModel(); 
        var shellView = new ShellView();    
        shellView.DataContext = shellVM;  
        shellView.Show(); 
    }
    

    This is usually the only place I set a DataContext for a UI component directly. From this point on, your ViewModels are the application. Its important to keep this in mind when working with MVVM. Your Views are simply a user friendly interface that allows users to interact with the ViewModels. They’re not actually considered part of the application code.

    For example, your ShellViewModel may contain:

    • BoardViewModel CurrentBoard
    • UserViewModel CurrentUser
    • ICommand NewGameCommand
    • ICommand ExitCommand

    and your ShellView might contain something like this:

    <DockPanel>
        <Button Command="{Binding NewGameCommand}" 
                Content="New Game" DockPanel.Dock="Top" />
        <ContentControl Content="{Binding CurrentBoard}" />
    </DockPanel>
    

    This will actually render your BoardViewModel object into the UI as the ContentControl.Content. To specify how to draw your BoardViewModel, you can either specify a DataTemplate in ContentControl.ContentTemplate, or use implicit DataTemplates.

    An implicit DataTemplate is simply a DataTemplate for a class that doesn’t have an x:Key associated with it. WPF will use this template anytime it encounters an object of the specified class in the UI.

    So using

    <Window.Resources>
        <DataTemplate DataType="{x:Type local:BoardViewModel}">
            <local:BoardView />
        </DataTemplate>
    </Window.Resources>
    

    will mean that instead of drawing

    <ContentControl>
        BoardViewModel
    </ContentControl>
    

    it will draw

    <ContentControl>
        <local:BoardView />
    </ContentControl>
    

    Now the BoardView could contain something like

    <ItemsControl ItemsSource="{Binding Squares}">
        <ItemsControl.ItemTemplate>
            <ItemsPanelTemplate>
                <UniformGrid Rows="3" Columns="3" />
            </ItemsPanelTemplate>
        <ItemsControl.ItemTemplate>
    </ItemsControl>
    

    and it would draw a board using a 3×3 UniformGrid, with each cell containing the contents of your Squares array. If your BoardViewModel.Squares property happened to be an array of TileModel objects, then each grid cell would contain a TileModel, and you could again use an implicit DataTemplate to tell WPF how to draw each TileModel

    Now as for how your ViewModel gets its actual data objects, that’s up to you. I prefer to abstract all data access behind a class such as a Repository, and have my ViewModel simply call something like SodokuRepository.GetSavedGame(gameId);. It makes the application easy to test and maintain.

    However you get your data, keep in mind that the ViewModel and Models are your application, so they should be responsible for getting data. Don’t do that in the View. Personally I like keeping my Model layer for plain objects that hold data only, so only ever perform data access operations from my ViewModels.

    For communication between ViewModels, I actually have an article on my blog about that. To summarize, use a messaging system such as Microsoft Prism’s EventAggregator or MVVM Light’s Messenger. They work like a kind of paging system: any class can subscribe to receive messages of a specific type, and any class can broadcast messages.

    For example, your ShellViewModel might subscribe to receive ExitProgram messages and close the application when it hears one, and you can broadcast an ExitProgram message from anywhere in your application.

    I suppose another method would be to just attach handlers from one class to another, such as calling CurrentBoardViewModel.ExitCommand += Exit; from the ShellViewModel, but I find that messy and prefer using a messaging system.

    Anyways, I hope that answers some of your questions and will point you in the right direction. Goodluck with your project 🙂

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

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to

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.