1
votes

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

1

1 Answers

1
votes

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