public DialogWindowView(DialogViewModel dialogViewModel, Window window) { if (window == null || window.DataContext != dialogViewModel) { return; } DialogWindow = window; WindowViewModel = dialogViewModel; IsValid = true; }
public DialogUserControlView(DialogViewModel dialogViewModel, UserControl userControl) { if (userControl == null || dialogViewModel == null) { return; } if (userControl.DataContext != dialogViewModel) { userControl.DataContext = dialogViewModel; } BaseDialogWindowView = new DialogBaseWindow(userControl, dialogViewModel); UserControlView = userControl; BaseDialogWindowViewModel = dialogViewModel; IsValid = true; }
public DialogBaseWindow(UserControl userControl, DialogViewModel dialogViewModel) { try { pDialogViewModel = dialogViewModel; this.DataContext = dialogViewModel; InitializeComponent(); try { // These are some conditions that are needed for everything to work correctly. if (userControl == null || userControl.DataContext != dialogViewModel) { // NOTE: This will cause the following suppressed error that only shows in debugger output: // 'System.InvalidOperationException' in PresentationFramework.dll // This happens since the window is already closed before a Show() or ShowDialog() method can actually do anything. this.Close(); return; } } catch (Exception Ex) { LogMVVM.Exception("MVVM Exception: Dialog Window user control and data context check error.", Ex); } try { if (dialogViewModel.Data.WindowIconURI.Length > 0) { Icon = BitmapFrame.Create(new Uri(dialogViewModel.Data.WindowIconURI, UriKind.RelativeOrAbsolute)); } } catch (Exception Ex) { LogMVVM.Exception("MVVM Exception: Dialog Window title bar icon creation error.", Ex); } // General Window properties. Not bound since some are not technically content and one is not able to be bound. WindowStartupLocation = dialogViewModel.Data.DialogStartupLocation; // Cannot be bound since it is a DependencyProperty WindowStyle = dialogViewModel.Data.DialogWindowStyle; Topmost = dialogViewModel.Data.Topmost; Title = dialogViewModel.Data.WindowTitle; Background = dialogViewModel.Data.Background; // Adds the user control to the window. this.dialogBaseWindowGrid.Children.Add(userControl); // If there is a specified control to focus, this will process that. FocusElementControl(dialogViewModel.Data.FocusUIElement); Loaded += contentLoaded; ContentRendered += contentRendered; Closing += windowClosing; dialogViewModel.Events.CloseDialogHandler += closeDialog; dialogViewModel.Events.SendMessageHandler += openWindowMessage; } catch (Exception Ex) { MessageBox.Show(this, "Window load error: " + Environment.NewLine + Convert.ToString(Ex), "Error...", MessageBoxButton.OK, MessageBoxImage.Error); } }
// Primary method for opening a dialog. private static WindowMessageResult openDialogWork(Window dialogWindow, DialogViewModel dialogWindowViewModel, Window parentWindow, ShutdownMode shutdownMode) { bool applicationUIThreadAccess = CheckApplicationUIThreadAccess(); if (applicationUIThreadAccess && shutdownMode != CurrentApplicationShutdownMode()) { // Param shutdownMode can used to prevent the Application.Current from going null. It can be turned off by setting ApplicationExplicitShutdown. // This should not throw. Possible investigation should be made to check on this. Application.Current.ShutdownMode = shutdownMode; } // We need to find a dispatcher to display this message. Dispatcher dispatcher = null; if (parentWindow == null && applicationUIThreadAccess == false) { return(WindowMessageResult.Undefined); } else if (parentWindow == null && applicationUIThreadAccess) { try { dialogWindow.Owner = Application.Current.Windows.OfType <Window>().FirstOrDefault(x => x.IsActive); } catch (Exception Ex) { LogMVVM.Exception("MVVM Exception: Dialog Service error when trying to get window owner.", Ex); // This should not throw. Possible investigation should be made to check on this. if (Application.Current.Windows.Count > 0) { dialogWindow.Owner = Application.Current.Windows[0]; } else { dialogWindow.Owner = null; } } if (dialogWindow.Owner == null) { dispatcher = Application.Current.Dispatcher; } else { dispatcher = dialogWindow.Owner.Dispatcher; } } else // This should ONLY happen if parentWindow is NOT null. { dialogWindow.Owner = parentWindow; dispatcher = parentWindow.Dispatcher; } try { if (dispatcher.CheckAccess() == false) { return(WindowMessageResult.Undefined); } WindowMessageResult result = WindowMessageResult.Undefined; dispatcher.Invoke((Action) delegate { dialogWindow.ShowDialog(); result = dialogWindowViewModel.Result; }); return(result); } catch (Exception Ex) { LogMVVM.Exception("MVVM Exception: Dialog service error when opening dialog window.", Ex); return(WindowMessageResult.Undefined); } }