How to programmatically move the main window in Prism WPF In C#

Topics: Prism v4 - WPF 4
Jul 25, 2014 at 6:48 PM
I was hoping to center the shell (main window) by overriding InitializeShell() with some lines like

Application.Current.MainWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;

However, there is no effect on moving the main window. I checked the XAML file, no property was set to prevent the main window from being moved.

Does everyone knows what I need to do for moving the main window?

Aug 13, 2014 at 5:11 PM
I do not think this is a prism problem. I would use an attached behavior and viewmodel pair to resolve this problem.
The behavior could bind to an attached property that is based on an event that would indicate when the window is to be re-positioned.

You could also do the same with window class that inherits from Window but does evaluate its DataContext Changed event to
react on a Move Window event that could be triggered by the viewmodel. I am talking about something like this:

I know this control is not a Window but it implements the pattern that I am referring to. It evaluates the DataContextChanged
event and filters an interface out of the viewmodel if one was attached:

The interface contains a events that are fired by the viewmodel:

Hope this helps.
Aug 15, 2014 at 7:53 PM
Edited Aug 15, 2014 at 7:54 PM
First, thanks so much for your reply.

Yes, it is not a prism problem. It is so odd that it was WindowState="Maximized" ResizeMode="CanMinimize" in my project XAML file that prevents me from resizing and moving my main window. The main window is just a shell which holds many user controls that in turn bound with a view model.

I never thought about these attributes have such effect on resizing and reposition. I tried to change style with different setters, all my efforts failed. By trial and error,
I delete WindowState="Maximized" ResizeMode="CanMinimize" from my project XAML, then I got what I wanted---moving the main window, centering it.

Could Anyone explain why WindowState="Maximized" ResizeMode="CanMinimize" in my project XAML can prevent me from resizing and moving my main window?