kunal-chowdhury.com

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.



Kunal Chowdhury
If you have come this far, it means that you liked what you are reading (How to Reposition a WPF Modal Window at the center of the Screen?).
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.

Don't forget to share your feedback/queries in the comment section below.