Domanda

yesterday i used google to find a few ways to make an awesome reusable modal dialog in WPF with PRISM 4.1 and the MVVM pattern. I found some examples but i must say non of those were as "pretty" as i liked them to be.

This one: WPF Modal Dialog (no mvvm -> no use)

This is pretty nice: Showing Dialogs when using the MVVM Pattern (but still it's using a selfmade ServiceLocator which i don't need as i am using the IUnity Container. I could use the logic and rewrite it to Unity but that's not the "pretty" way in my honest opinion.

Well after a while searching the web for informations some blog (can't find the source right now) told me that the PRISM Framework got something called "interaction requests". So i checked out the prism documentation and found a small part under the topic "advanced mvvm scenarios" but the information given in the documentation aren't enough.

I'd like to know if somebody have any good example or any good blogpost about how to realize an awesome modal dialog in prism wpf with mvvm.

EDIT: Regarding the question in the comments:

What makes a modal dialog awesome?

Indeed a good question.

  1. It must be modal (while the dialog is open the rest of the UI should be freezed)
  2. The dialog view can have it's own viewmodel or at least i would like to give an instance of an object to the dialog view and return an object back to the parent view
  3. The view should be an own "xaml" file
  4. the dialogresult feature from .NET or at least a way to get a response what the user clicked in the dialog
È stato utile?

Soluzione

PRISM 5.0 came up with quick solution to show modal dialogs. Using PopupWindowAction.

<prism:InteractionRequestTrigger SourceObject="{Binding CustomPopupViewRequest, Mode=OneWay}">
    <prism:PopupWindowAction>
        <prism:PopupWindowAction.WindowContent>
            <views:CustomPopupView />
        </prism:PopupWindowAction.WindowContent>
    </prism:PopupWindowAction>
</prism:InteractionRequestTrigger>

Altri suggerimenti

Interaction requests require a little more up-front work, but they are definitely the right way to go from the MVVM purist perspective...

I saw an example of how to do this with Prism in Karl Shifflett's MVVM In The Box training extension.

As I remember, the example was pretty rough around the edges, but it should set you in the right direction.

The problem with this kind of in-view "Dialog" is it doesn't allow the dialog to go outside the bounds of the parent window. On the plus side, you can do a lot of fancy layout and animation stuff.

check my post from here

its simple, its mvvm, its a service and "all you have to do" in your viewmodel is:

var result = this.uiDialogService.ShowDialog("Dialogwindow title goes here", dialogwindowVM);

Caveat: I have not used PRISM and my answer assumes the use of just WPF and MVVM. I don't see this as a major problem as your list of requirements can be met without PRISM (which can be added on top of the basic solution at a later date anyway).

I have a project on Github which provides a custom FrameworkElement called ModalContentPresenter that allows modal content to be displayed. The element basically consists of two panes, one layered on top of the other. The back pane hosts your main content and the front pane hosts your modal content. The element has a dependency property which controls if the modal content is shown.

The element only provides the basic 'modal' functionality and is capable of hosting arbitrary content (like most WPF controls). If, for example, the modal content you are displaying is to look and behave like a window (have a title, close button, mouse drag etc.) then you will still need to do some work.

Here is how the ModalContentPresenter can address your requirements:

It must be modal (while the dialog is open the rest of the UI should be freezed)

The ModalcontentPresenter can be placed at any level within your visual hierarchy and anything behind the modal content (when displayed) will be inaccessible. The controls will still be enabled and will still react to any changes in the viewModel they are bound to but the user will be unable to navigate and interact with the controls using the mouse and keyboard.

The dialog view can have it's own viewmodel or at least I would like to give an instance of an object to the dialog view and return an object back to the parent view.

This Stackoverflow answer shows how I would recommend you achieve this.

The view should be an own "xaml" file

Both the primary and modal content can be defined using inline xaml or separate xaml files (such as a UserControl).

the dialogresult feature from .NET or at least a way to get a response what the user clicked in the dialog

The linked answer above shows how to get an 'answer' from your modal content. The basic premise is that your viewModels communicate normally (either directly or via other means such as an event bus). The only difference is that you just happen to be displaying your content in a way which means the user can only interact with the 'modal' data.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top