Error window show modal in MVVM WPF

前端 未结 6 2236
半阙折子戏
半阙折子戏 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:36

    I am also working on a MVVM project where I need modal dialogboxes or messageboxes. I have found the following way of solving it:

    The software uses only one window. The layout root element is a Grid with no row- or columndefinitions. The grid has three children:

    1. A dockpanel that contains all the usual stuff like menus, tabbed views, status bar and so on.
    2. A grid that has a gray background and a 50% opacity. This is used as a veil to cover the dockpanel when a modal box is in effect. The veil grid is usually collapsed.
    3. A grid holding modal views, this is usually collapsed.

    The viewmodel for the main window has a member called Modal. If this is null, the two grids for modal use are collapsed through databinding and a converter for Visibility.Collapsed.

    When the program wants to display for example a modal message box, a MessageBoxViewModel is instantiated and assigned to MainViewModel.Modal. The MessageBoxViewModel has a Command for an OK-button. This Command raises an event that sets the MainViewModel.Modal to null again.

    The veil grid occludes the main DockPanel, so that no controls outside the Modal control accept input.

    Your program can either run a messagepump until OK is pressed, or the OK-Command can trigger the next. There are many ways of solving different needs, but the Model-ModelView solution should support them.

    I feel that this is as good a model of the view in the modal mode as one can hope for.

提交回复
热议问题