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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:26:20+00:00 2026-05-18T00:26:20+00:00

I am writing a small application to learn WPF/MVVM and I have sort of

  • 0

I am writing a small application to learn WPF/MVVM and I have sort of run into a conundrum, I have a viewmodel object ready to go, but I don’t know where to instance it.

The viewmodel represents a single windows data(some sliders values, a progressbar value and a few textstrings. Some of these are directly attached to the model which is exposed, others are in the viewmodel to avoid adding new functionality to the model.)

I will only ever need 1 of these objects at a time(per window, but I am only allowing 1 window), although it really isn’t a singleton. It will exist for the lifetime of the window though(is that normal?)

So my question is this: Should I instance the viewmodel as a static resource in the App.Xaml, as a member of the App.xaml.cs class in the code behind(inside of an overridden “OnStartup” method) or as a resource in the Window.xaml, or as an object in the Window.xaml.cs.

I have seen people put it as a local resource and as a global object in the startup, but to me it seems like it shouldn’t be in the code behind(all I am doing is throwing it up in the air, once it’s in existance it can take care of everything else. That’s the whole point of it, actually).

So thoughts on where the viewmodel should be instanced?

  • 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-18T00:26:21+00:00Added an answer on May 18, 2026 at 12:26 am

    It’s completely normal for a window’s view model to exist only for the lifetime of its window. Creating it can be as simple as putting:

    DataContext = new MainWindowViewModel();
    

    in the constructor for MainWindow. That’s how I do it in the absence of a compelling reason not to. (If the window’s going to need to interoperate with the view model in its event handlers, which it sometimes does, I’ll create a private field for the object so that I don’t have to keep casting DataContext in all the event handlers.)

    Usually, the view model needs to interoperate with one or more domain objects. The challenge in that case is coming up with a way of telling the view model about that object without coupling the domain object to the window that’s creating the view model. This is where you start screwing around with services and service locators and mockable objects and the like.

    But even when you do this, you can still create the view model simply in the window’s constructor, e.g.:

    ServiceLocator loc = (ServiceLocator) FindResource("AppServiceLocator");
    DataContext = new MainWindowViewModel(loc);
    

    The only time I don’t create a window’s view model in the window’s constructor is when the some other object – like a command in another window – is creating the window and setting its DataContext.

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

Sidebar

Related Questions

I'm writing a small application in VB.NET and I would like some of the
I've been writing writing a small pygtk application using glade to put together the
I'm writng a small application in PHP + MySQL and have come to the
I am developing a Java Desktop Application but have some confusions in choosing a
I am currently writing a small application in Windows Mobile using CF.NET. The application
I just started writing a small application in C++ using Visual Studio C++ 2008
I am writing a small application to get the various diagnostic parameter of Apache
I am writing small social application. One of the features is to write user
I'm writing a small application in C that reads a simple text file and
I am writing a small application using Rails 3. In one part of it

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.