/// <summary> /// Create a matching view for a given ViewModel instance /// </summary> /// <typeparam name="Type">The type of the ViewModel</typeparam> /// <param name="viewModel">The instance of the ViewModel</param> /// <param name="key">The View identifiert (see AssociatedViewModelAttribute)</param> /// <returns>An AbstractView instance if successful, null otherwise</returns> public AbstractView ShowModal <Type>(Type viewModel, string key = null) { AbstractView abstractView = null; if (_viewModelToViewMappings == null || !_viewModelToViewMappings.ContainsKey(typeof(Type))) { BuildOrUpdateAttributeDictionary(); } List <TargetView> possibleViews = null; if (!_viewModelToViewMappings.TryGetValue(typeof(Type), out possibleViews)) { throw new ArgumentException("Unknown ViewModel type"); } possibleViews = possibleViews.Where(view => view.AssociatedViewModel.IsModal).ToList(); if (possibleViews.Count == 0) { throw new ArgumentException("No modal view registered for this type of view model"); } TargetView selectedView = null; if (key == null) { if (possibleViews.Count == 1) { selectedView = possibleViews.First(); } else { var defaultView = possibleViews.Where(item => item.AssociatedViewModel.IsDefault).FirstOrDefault(); if (defaultView != null) { selectedView = defaultView; } else { selectedView = possibleViews.FirstOrDefault(); } } } else { selectedView = possibleViews.Where(item => item.AssociatedViewModel.Key == key).FirstOrDefault(); } if (selectedView.CustomCreateMethod != null) { abstractView = selectedView.CustomCreateMethod(viewModel); } else { ContentControl result = null; if (selectedView.AssociatedViewModel.FactoryMethod != null) { result = selectedView.AssociatedViewModel.FactoryMethod(); } else { result = Activator.CreateInstance(selectedView.View) as ContentControl; } if (result != null) { result.DataContext = viewModel; } abstractView = new AbstractView(result); } abstractView.View.Loaded += (sender, e) => abstractView.View.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); if (BeforeModalDialog != null) { BeforeModalDialog(abstractView.View, null); } if (WindowCreated != null) { WindowCreated(abstractView.View, null); } SetUnmanagedOwnerIfNecessary(abstractView.View as Window); (abstractView.View as Window).ShowDialog(); if (AfterModalDialog != null) { AfterModalDialog(abstractView.View, null); } return(abstractView); }