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?