How to Reposition a WPF Modal Window at the center of the Screen?


In my earlier post “How to Reposition a Silverlight Child Window?”, I demonstrated you how to reposition a Silverlight Child Window into the screen. I tried the same with the WPF Modal Window too. But was not able to make it.

 

Later Thomas Claudiush gave me a cool sample where he did it very easily. In this post, I am going to share that code for you so that, if in near future you need the same behavior, you don’t have to research for it again.

 

To begin with, read my previous post “How to Reposition a Silverlight Child Window?” which will give you some basic idea on what we are going to do. Lets discuss the same once again. Suppose, we have a Modal Window instance in our application and we are showing the window by calling the ShowDialog() method of the window instance. This method will open the window as Modal. You can’t call Close() on it. If you call and later want to reopen it again, it will through Exception of type InvalidOperationException.

 

image

 

So, what to do? We have to call the Hide() method to close the modal window. As the window is hidden in the screen, we can call the ShowDialog() again to show the Window.

 

In such case you will notice that, the window is not opening at the center of the screen. Different time it is opening in various location. If you drag the Window to a specific location, next time it loads in that place only but you need to show the window at the center of the screen.

 

Here, I created an Extension class for Window having the method to center it in the screen. Have a look into the code:

 

 
    /// < summary>
    /// Extension class to do some extra operation with Window
    /// </summary>
    public static class WindowExtensions
    {
        /// <summary>
        /// Static local variable to check whether it is the first instance
        /// </summary>
        private static bool m_firstTime = true;
 
        /// <summary>
        /// Centers the Window in screen.
        /// </summary>
        /// <param name="window">The window.</param>
        public static void CenterInScreen(this Window window)
        {
            double width = window.ActualWidth;
            double height = window.ActualHeight;
 
            if (m_firstTime) // First time ActualWidth and ActualHeight is not set
            {
                if (!Double.IsNaN(window.Width)) { width = window.Width; }
                if (!Double.IsNaN(window.Height)) { height = window.Height; }
 
                m_firstTime = false;
            }
 
            // Set Left and Top manually and calculate center of screen.
            window.Left = (SystemParameters.WorkArea.Width - width) / 2 
                + SystemParameters.WorkArea.Left;
            window.Top = (SystemParameters.WorkArea.Height - height) / 2
                + SystemParameters.WorkArea.Top;
        }
    }

 

 

The above extension method will help you to call the CenterInScreen() method directly from the Window instance. Hope, this trick will help you when you work with the same.


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

4 comments

  1. I am not sure what is the requirement here. I believe a dialog will be shown within an application. So if we provide a owner of any window to the dialog, then we can use the CenterOwner property to show the dialog in the center. Am I missing something here?

    ReplyDelete
  2. Hi Vijay,

    You are right in one case. The WindowStartupPoisition property does the samething for you, if you are creating a new instance of the Window everytime.

    But, in our case, as I mentioned, we will use the same instance. Thus, if you call the ShowDialog() on the same instance of the Window, the WindowStartupPosition property will not work. It only works for the first time.

    This code will handle the same. Read the first trick for Silverlight. You will understand the reason behind this trick.

    Cheers...

    ~Kunal

    ReplyDelete
  3. Hi Kunal, in this code, the private static member will be always set to false... maybe shoud set the value to true in the static constructor.

    ReplyDelete
  4. Hi Jone,
    You are absolutely right on this. It will be set to true at the first time. Modified it accordingly. Thanks.

    ReplyDelete

 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top