Error window show modal in MVVM WPF - wpf

I have implemented my MVVM error message as a message dialog that subscribes to error messages via a mediator class, so that other viewmodels can notify it if any errors occur.
When an error occurs, I set the visibility attribute in the viewmodel to Visible, to display the error window. This is all bound in the Error window from the viewmodel.
However, this window is NOT modal! I need to show it as a dialog and not just set the visibility to true - is there any kind of binding I can do, even if I have to extend the functionality of the window? I'd rather not break MVVM if I can avoid it.
Thanks!

The View/ViewModel split is meant to divide look from functionality. I firmly believe the Window is functionality and look rolled into one. For instance, what if in your ErrorMessageViewModel, you had this code that executes when there are errors:
class WindowViewModel : Window
{
}
.
.
.
WindowViewModel newDialog = new WindowViewModel();
newDialog.Content = myErrorListViewModel;
newDialog.Parent = mainWindowViewModel;
newDialog.ShowDialog();
So the contents of the dialog is the ViewModel for your error list. Define your View as a data template that automatically applies itself to the error list ViewModel.
Doesn't that look like MVVM?
The fact is, the Window class is a ViewModel for the Window you see on the screen. By changing the properties of the Window object, it affects the "view" just like if the properties of the WindowView were bound to a WindowViewModel. The only thing missing is the ability to "restyle" the Window using WPF, and it doesn't matter how hard you try to implement it, you're not going to be able to do that. The user can restyle a Window by modifying their desktop theme, but you're not in control of it. The best you can do is turn off the chrome and/or make it full screen.

You find an example how windows (don't matter if they are modal or not) are shown, in the ViewModel example of this project:
WPF Application Framework (WAF)
http://waf.codeplex.com

I am also working on a MVVM project where I need modal dialogboxes or messageboxes. I have found the following way of solving it:
The software uses only one window. The layout root element is a Grid with no row- or columndefinitions. The grid has three children:
A dockpanel that contains all the usual stuff like menus, tabbed views, status bar and so on.
A grid that has a gray background and a 50% opacity. This is used as a veil to cover the dockpanel when a modal box is in effect. The veil grid is usually collapsed.
A grid holding modal views, this is usually collapsed.
The viewmodel for the main window has a member called Modal. If this is null, the two grids for modal use are collapsed through databinding and a converter for Visibility.Collapsed.
When the program wants to display for example a modal message box, a MessageBoxViewModel is instantiated and assigned to MainViewModel.Modal. The MessageBoxViewModel has a Command for an OK-button. This Command raises an event that sets the MainViewModel.Modal to null again.
The veil grid occludes the main DockPanel, so that no controls outside the Modal control accept input.
Your program can either run a messagepump until OK is pressed, or the OK-Command can trigger the next. There are many ways of solving different needs, but the Model-ModelView solution should support them.
I feel that this is as good a model of the view in the modal mode as one can hope for.

I made a behhavior to tie some modal dialogs to the command.
http://www.clr-namespace.com/post/MVVMModal-dialog-before-running-Command.aspx
<Confirm:Confirm IsConfirm="{Binding ElementName=checkBoxConfirm, Path=IsChecked}"
Command="{Binding Path=ButtonCommand}"
CommandParameter="{Binding ElementName=textBoxParameter, Path=Text}"
ConfirmMessage="Are you sure you want to fire the command?"
ConfirmCaption="Question" >
</Confirm:Confirm>

In my recent blog post you can find simple solution for Modal Dialogs and Message Boxes in MVVM for Silverlight but this can be simply reused in WPF:
Modal dialogs with MVVM and Silverlight 4

i'm using the same method as Scott Whitlock.
there is just one more important property to set:
class ModalDialog: Window
{
}
.
.
.
var dlg = new ModalDialog {
Content = viewModelName,
**TopMost = true,**
Parent = mainWindowViewModel
};
dlg.ShowDialog();

Related

WPF - modify window content from ViewModel

I have a WPF application. I'm trying to modify it to use the MVVM pattern.
Now, in the code behind of my main window, when I click a button, I change the "Content" of my window with a WebBrowser control :
Content = webBrowserWrapper.WebBrowser;
I would like to do it from a command in my ViewModel. How can I access the "Content" property of my window ?
If it's not possible, what is the simple way to modify the content of my window (maybe create a new window and navigate to it)
The principal behind MVVM is that UI is the concern of the View, and logic is the concern of the ViewModel.
You describe a concern of the View, which is properly handled within its codebehind. There is no need to involve the ViewModel in this action.
So, in other words... congrats. You're already there.

Update label content on usercontrol from Mainwindow - WPF

I am new to WPF.
I have requirement to create a window (as below. Ignore background color. Just gave color to distinguish different panels). On click on next, center panel (with frame) will load another page/user control. This is working fine.
Now I need to validate data/input of the form when user clicks Next. If everything is fine, next control should load. But I am unable to get controls from frame to Mainwindow.
Can someone please help me on how can I access controls of page/usercontrol from Main Window. I have a label after each control which will show error message if values are not not provided or are incorrect.
If you are using MVVM Pattern, On your next click, you can raise an event from the usercontrol View Model and get some response back to the Main Page's View Model where your Next code resides.
OR
You can access the Usercontrol's Datacontext in Main Page's View Model, Cast it to your Usercontrol's View Model. Take the information and just do your validation.
Hope this will help you...

WPF Open a Modal window from UserControl

I have a MainWindow in my WPF app.
This MainWindow has a menu on the left and when a menu option is selected a UserControl is loaded on the right.
This is similar to Master Pages in asp.net
What I want to do now is to have a modal window show from the UserControl which will only allow the user to interact with the modal window.
I have seen examples of the Main Window showing a modal window (http://www.codeproject.com/Articles/36516/WPF-Modal-Dialog) but not sure on how to load this from a UserControl.
There's this: dialogs and mvvm but this is the best example I've seen of dealing with it: mvvm and closing forms
The first link I've not used and stumbled across while looking for the second link to post that here. The second link has two downloads, you can ignore the _service download, it's basically the same.
One way in WPF is this method
Add a new Window to the project.
Add other controls onto the window as needed.
In XAML name the window such as x:Name="MyWindow"
Put on Dependency properties on the window and have each of the controls bind to the window's data context such as {Binding MyText, ElementName=MyWindow}. (Note I still use, even for WPF these Visual Studio code snippets to add different dependency properties, these save time for a very redundant operations of adding them: Silverlight Snippets.
In the location where you want to launch the model dialog use this example:
Example:
var about = new About(); // Create the new window
// I've added a CompanyName dependency property.
about.CompanyName = "OmegaMan Industries";
about.ShowDialog();

Closing WPF dialog box from contained UserControl

Hi I have a WPF application with various UserControls that contain key functionality. I want to be able to show say a FileManager UserControl in a tab on the main application, or have a dialog pop up when required, that contains the same UserControl.
It seemed like a good idea to create a modal Window and set its Content to the FileManager Usercontrol. But when I am finished with it, I am not sure how to close the containing Window, using a button on the UserControl. Is there an elegant way of doing this without having to store a reference to the Window in the UserControl?
Thanks for any advice!
Create an Event which is called when the respective button on the user control is clicked. That way, the containing control can react to the event in the appropriate manner. For example, a dialog could just close itself.
Is closing a window something that is integral to the functionality of the control in all the contexts where the control is hosted? E.g Does closing a window apply to the case where the control is hosted in a tab of the main app?
If not then you might be better off separating window closing code out of the UserControl in into the window/dialog that is hosting it - using events or whatever to tie the two together.

Hiding Popup control when other window's in focus

I have a custom UserControl which tries to recreate auto-complete for a textbox. When user types, the text is used to filter a provided collection of items and then a Popup displays a ListBox with items that match what user have typed.
Unfortunately, if user decides to switch away from the application to another window (browser, MSWord, anything!), the Popup remains on top of every other window!
Also, if I move my window (which hosts the custom control) with the popup open, the popup stays in place (and doesn't follow the window)! It's kinda funny but obviously not acceptable behaviour. I've looked around but only found one post about this that went unanswered for two years :(
Actually, I didn't realize that I had StaysOpen property of the Popup set to true.
<Popup StaysOpen="False" />
actually does the trick for me.
I had the same problem in a similar scenario. What I did was I subscribed to all posible "lost focus" events of the control and also got the window which hosts the control and subscribed to its GotMouseCapture and LocationChanged events. Event handlers of all those events are setting the popup's IsOpen property to false.
You can get the hosting window with this:
parentWindow = Window.GetWindow(this);
all other code is simply a lot of subscribing to events to do the same thing.
P.S. I'm not saying it's a pretty or optimal solution, but it works fine for me :)
According to the Popup documentation:
When Popup is displayed on the screen, it does not reposition itself if its parent is repositioned.
So it does not look like it would be a very good candidate for an autocomplete textbox. I think the class is meant more for showing information when you hover over an item.

Resources