1

I know there are a lots of samples unig region manages and interaction request triggers to show dialogs using prism, WPF and MVVM. in my situation I have different modules. For example I have module which creates organization units and assigns executive person to newly created unit. this view needs to be shown in standard dialog window with returned object of Model type which is OrganizationUnit object in this case. after showing view in modal window it has an textbox input here executive person name is entered and in case no matching person found it needs to open another dialog window which creates person and returns created object to already opened dialog window. I have seen all examples of using dialogs in MVVM as well as using popup region behavior and interactionrequesttrigger in PRISM. I do not get much out of using custom control to show popup as all I need is standard showdialog method which can be called from any viewmodel with callback specified.

the way I want to achieve this is to have interactionrequesttriger in my shell and initial call to open popup dialog is performed on this trigger. to not to make this all very complicated I want some centralized way of showing dialogs and within dialogs showing other dialogs and all this with callbacks. could someone point me to the right direction?

By The way I want to have single control DialogView which will have ContentControl where viewmodels inject their views and buttons placeholder which holds buttons; Ok, Cancel, Apply and want to bind have them bind to loaded viewmodels commands.

I know all this sounds a little bit complicated and complex but there might be solution for this. please someone give me ideas of how to accomplish this task

H.B.
  • 166,899
  • 29
  • 327
  • 400
Rati_Ge
  • 1,262
  • 2
  • 15
  • 37

1 Answers1

1

I spent a lot of time looking at interaction requests with requirements nearly identical to yours. In the end, i stopped using them because they introduced a lot of complexity to what traditionally has been a very simple task.

Instead i use what i call a UIService, that is based on an interface of IUIService, so that the GUI part can be mocked if you want to run automated testing.

Each module has its own UIService and the shell has a common UIService that any module can access via IShellUIService.

The advantage of making a ui service is that you can use traditional dialog functionality and make it completely mvvm compliant as well as be able to swap out the traditional dialog functionality for other mechanisms if need be.

example UIService

public class UIService : IUIService
{
    public UIService(IUnityContainer container)
        : base(container)
    {
    }

    public ViewModelBase DialogBoxUserDetails(ViewModelBase viewModel)
    {
        ShowDialog(new UserDetailsView(), ref viewModel);
        return viewModel;
    }

protected bool? ShowDialog(FrameworkElement view, ref ViewModelBase viewModel)
    {
        Window w = new Window();

        //make sure the new dialogbox belongs to the parent window
        HwndSource winformWindow = (System.Windows.Interop.HwndSource.FromDependencyObject(Application.Current.Windows[0]) as System.Windows.Interop.HwndSource);
        var interopHelperAdd = new WindowInteropHelper(w) { Owner = winformWindow.Handle };

        //use the applications wpf theme
        w.Resources = Application.Current.Windows[0].Resources;

        w.DataContext = viewModel;
        w.Content = view;


        bool? bResult = w.ShowDialog();

        if (bResult != true)
        {
            viewModel.Dispose();
            viewModel = null;

        }

        return bResult;
    }

}

The above updates your sets the vm to null if the dialogbox is cancelled or closed, else the updated vm is returned

For information on connecting your cancel button to the dialog close in a mvvm friendly way check out this answer

Community
  • 1
  • 1
Anton
  • 7,709
  • 5
  • 31
  • 33
  • There are some really interesting pints in your solution but one detail When window is shown I do not want to create Each view with buttons I want to have one control which has structure like this and after this place viewmodels view in top row and or hide predefined buttons in bottom row and want to be able to bind OK Cancel Apply commands to each button based on viewmodels implementation so what I will get is some sort of reusable window – Rati_Ge Jan 25 '12 at 14:48
  • i have predefined buttons defined as usercontrols that i "drop into" the view that i want into the dialogbox. It works well, no extra logic required to hide or show buttons depending on what vm is used. – Anton Jan 26 '12 at 23:18
  • 1
    I have been looking to implement almost identical functionality in a Prism application also using regions and modules and in the end had to give up and implement a dialog service provider as well. Most of the samples out their deal with raising a modal dialog within the context of the current view, which if you are using modules and regions is not ideal. The UI Service pattern is actually mentioned in this Prism chapter, http://msdn.microsoft.com/en-us/library/gg405494%28v=pandp.40%29.aspx under "Using an Interaction Service" – nrjohnstone Feb 18 '14 at 20:45