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.
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:
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.