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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:34:11+00:00 2026-05-23T03:34:11+00:00

I have a ViewModel called MainWindowViewModel. In this I have a property that shows

  • 0

I have a ViewModel called MainWindowViewModel. In this I have a property that shows a modal window when an error occurs. This works fine as long as an error occurs during start-up.

When an error occurs after start-up, in SubViewModel, I invoke the parametrized constructor in MainWindowViewModel.

MainWindowViewModel.cs

public MainWindowViewModel()
{
    if (!isServerRunning)
    {
        this.ModalWindow = new LogViewModel("Server is down.");
    }
    else
    {
        this.ModalWindow = new LogViewModel();
    }
}


public MainWindowViewModel(string logMessage)
{
    this.ModalWindow = new LogViewModel(logMessage);
}


public LogViewModel ModalWindow
{
    get
    {
        return _modalWindow;
    }
    set
    {
        _modalWindow = value;

        OnPropertyChanged("ModalWindow");
     }
}

MainWindow.xaml

….

<Grid>
    <vw:LogView Content="{Binding Path=ModalWindow}"/>
</Grid>
  • MainWindowViewModel is bound to MainWindow.xaml

  • SubViewModel is bound to SubView.xaml

  • MainWindow.xaml loads multiple views, one of them is SubView.

In App.xaml I have created an instance of the ViewModel and bound it to MainWindow.

protected override void OnStartup(StartupEventArgs e)
{                                  
    base.OnStartup(e);
    MainWindow mainWindow = new MainWindow();
    MainWindowViewModel viewModel = new MainWindowViewModel();
    mainWindow.DataContext = viewModel;
    mainWindow.Show();
}

What I realized was that the modal window shows up when an error occurs after start-up if I create the modal window property in SubViewModel and bind it to SubView. But this is not ok since SubView is only a DockPanel covering 1/4 of the MainWindow. I.e. 1/4 is only covered with a modal window instead of the whole MainWindow.

I am not sure why modal window does not appear in MainWindow when I call the parametrized constructor from SubViewModel. When I debug I see that the part _modalWindow = value; has correct values but in any case the modal window does not show up.

I am binding the ViewModel to the Datacontext of the MainWindow. That’s is probably why I see the Modal window when error occurs on start-up. For errors after start-up: Must I (from SubViewModel where I invoke the parametrized constructor in MainWindowViewModel) do some kind of binding to the datacontext of the Mainwindow again? How is the best way of doing this without having to create a new instance of MainWindow? Because MainWindow should only be created once.

Any help is much appreciated.

  • 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-23T03:34:12+00:00Added an answer on May 23, 2026 at 3:34 am

    It looks like you are recreateing the ViewModel every time an error occours. If so you would need to reset the binding in the view as well, which would defeat the purpose of MVVM.
    Rather have one instance of the ViewModel and propagate the errors to the View using OnPropertyChanged().

    There are of course many ways of doing this, but I usually keep a reference to the ViewModel in my View, and then a reference to the Model in the ViewModel. This way the Model is completely decoupled from the View/ViewModel and likewise the ViewModel is decoupled from the View.

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

Sidebar

Related Questions

I have a textbox that is bound to a property on my ViewModel called
I Have a controller that returns a viewModel. That viewModel has a property called
In my ASP.NET MVC2 application, I have a ViewModel class called UserCreateViewModel. In this
In my SilverLight application, I have a property in my ViewModel called 'vmProperty' and
I have a viewmodel which implement INotifyPropertyChanged . On this viewModel is a property
I have a ViewModel class that implements the IDataErrorInfo Interface . In each property's
I have a TextBox that is bound to my ViewModel. The TextWrapping property of
I have a viewmodel called ArticleAdmin that includes a list of checkboxes: public class
I have a ViewModel class that looks like this. class MyViewModel : Screen {
I have a TextBox.TextProperty bound to a ViewModel property. In the binding I have

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.