Error window show modal in MVVM WPF

前端 未结 6 2238
半阙折子戏
半阙折子戏 2021-01-02 15:25

I have implemented my MVVM error message as a message dialog that subscribes to error messages via a mediator class, so that other viewmodels can notify it if any errors occ

6条回答
  •  梦谈多话
    2021-01-02 15:58

    The View/ViewModel split is meant to divide look from functionality. I firmly believe the Window is functionality and look rolled into one. For instance, what if in your ErrorMessageViewModel, you had this code that executes when there are errors:

    class WindowViewModel : Window
    {
    }
    
    .
    .
    .
    
    WindowViewModel newDialog = new WindowViewModel();
    newDialog.Content = myErrorListViewModel;
    newDialog.Parent = mainWindowViewModel;
    newDialog.ShowDialog();
    

    So the contents of the dialog is the ViewModel for your error list. Define your View as a data template that automatically applies itself to the error list ViewModel.

    Doesn't that look like MVVM?

    The fact is, the Window class is a ViewModel for the Window you see on the screen. By changing the properties of the Window object, it affects the "view" just like if the properties of the WindowView were bound to a WindowViewModel. The only thing missing is the ability to "restyle" the Window using WPF, and it doesn't matter how hard you try to implement it, you're not going to be able to do that. The user can restyle a Window by modifying their desktop theme, but you're not in control of it. The best you can do is turn off the chrome and/or make it full screen.

提交回复
热议问题