-1

I tried to find a way to open dialog boxes from an MVVM application, but wasn't satisfied with the suggested methods.

I ended up writing an action that does that:

public class OpenWindowAction : TriggerAction<DependencyObject>
{
    public OpenWindowAction()
    {
        ShowDialog = false;
    }

    [AutoDependencyProperty]
    public Type WindowType { get; set; }

    [AutoDependencyProperty]
    public object DataContext { get; set; }

    [AutoDependencyProperty]
    public bool ShowDialog { get; set; }

    protected override void Invoke(object parameter)
    {
        if (WindowType.IsSubclassOf(typeof(Window)))
        {
            var window = (Window)Activator.CreateInstance(WindowType);
            window.DataContext = DataContext;

            if (ShowDialog)
                window.ShowDialog();
            else
                window.Show();
        }
    }
}

Usage:

<i:Interaction.Triggers>
  <i:EventTrigger EventName="MouseDoubleClick">
    <x:OpenWindowAction DataContext="{Binding ...}" ShowDialog="True" WindowType="x:EditWindow" />
  </i:EventTrigger>
</i:Interaction.Triggers>

Am I doing it right?

omni96
  • 41
  • 1
  • 10
  • http://stackoverflow.com/questions/3801681/good-or-bad-practice-for-dialogs-in-wpf-with-mvvm – blindmeis Apr 25 '17 at 09:27
  • @blindmeis That's nice but it has to go through the ViewModel. I'm not sure this is the best practice – omni96 Apr 25 '17 at 09:41
  • i dont know if its best practice. but how do you unit test your eventtrigger dialog open stuff ;) a dialog is for somekind of logic and thats i why i want this in my viewmodels. – blindmeis Apr 27 '17 at 14:01

1 Answers1

0

Am I doing it right?

Well, yes. This is very similar to the way Prism handles dialog/notification request. You could refer to the official sample here: https://github.com/PrismLibrary/Prism-Samples-Wpf/tree/master/25-NotificationRequest/UsingPopupWindowAction

...and the PopupWindowAction class here: https://github.com/PrismLibrary/Prism/blob/b9caefffa7cec59cfea7217cce244437408c87ad/Source/Wpf/Prism.Wpf/Interactivity/PopupWindowAction.cs

The following blog post might also be helpful: https://blog.magnusmontin.net/2013/04/20/implement-a-confirmation-dialog-in-wpf-with-mvvm-and-prism/

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Do you know of any similar implementations using RectiveUI? – omni96 Apr 25 '17 at 14:53
  • ReactiveUI has no concept of dialogs. But please don't ask additional questions in the comments section. Please accept the answer if your original question has been answered and then ask a new question if you have another issue. – mm8 Apr 25 '17 at 14:57