Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have a modal dialog shown on click of a button in the main form

using the ShowDialog method i am able to show the child modal dialog on top of my main form.
But if i click outside my application suppose if i select some other application and come back to my application .. the main form is focused..

How shall i make my Child modal dialog focused when my application is seleced ??
Thanks in advance
Posted
Comments
[no name] 8-Aug-12 11:46am    
Sounds like you are not really showing the dialog as modal.
Sergey Alexandrovich Kryukov 8-Aug-12 13:50pm    
I suspect that, too.
--SA
arun_pk 8-Aug-12 11:48am    
private void button1_Click(object sender, RoutedEventArgs e)
{
Window1 aa = new Window1();
aa.ShowDialog();
}

i am doing it in this way
[no name] 8-Aug-12 11:57am    
That code behaves exactly as it is supposed to on my system.
Sergey Alexandrovich Kryukov 8-Aug-12 13:52pm    
OP asked a right question below...
--SA

1 solution

There are two Window properties which control the behavior of the activation of the application. If you use them properly, all the windows of the application will behave like one application, so no window of any other application cannot appear in between in Z order. This behavior will be achieved for all windows, not only the main and the modal ones.

First one is System.Windows.Window.Owner:
http://msdn.microsoft.com/en-us/library/system.windows.window.owner.aspx[^].

To achieve right behavior, you need to leave the main window not owned (naturally), and all other windows should be owned by the main one. More complex ownership relation is quite possible but makes not much sense.

The second one is not so important, it only adds some consistency: you should set the Boolean property ShowInTaskbar to true for the main window, false for all others. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.window.showintaskbar.aspx[^].

Now, let's see at the documentation on ShowDialog:
http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx[^].

MSDN says:
A window that is opened by calling the ShowDialog method does not automatically have a relationship with the window that opened it; specifically, the opened window does not know which window opened it. This relationship can be established using the Owner property and managed using the OwnedWindows property.


So, you need to set the property Owner of the modal window as well, to get expected behavior. Please try. I don't understand why this is not done through ShowDialog, automatically, but the fact is: WPF application does not provide a default way of knowing the main window (you can develop such code yourself), and the method ShowDialog does not have a parameter to indicate ownership.

Please try it. If it does not resolve your problem, it would mean you screw up something somewhere else, but I don't thinks so. Please report back if it helps or not.

—SA
 
Share this answer
 
v2
Comments
arun_pk 8-Aug-12 15:13pm    
SAThanks a lot.
Its working. My requirement is bit more complicated.

In my in window i have a user control .. inside the user control i have a button.
on click of that button i am opening a modal window..
So the showDialog method is been called inside the user control
so i cant set the Window.Owner = this
because the this pointer is pointed to the UserControl where as the Owner property is expecting a window..
how shall i set the owner of a window from a usercontrol?
Sergey Alexandrovich Kryukov 8-Aug-12 15:23pm    
You are welcome.
No, your requirements are just right, it's just that many other developers do not pay attention to important but small detail as you do, so I appreciate it.

Now, how to set an owner of a window in a usercontrol? Well, what's the problem? How to pass a reference to the owner (main window, presumably) to an instance of the user control?
Well, you can always create a write-only or read/write property of a user control specially for this purpose. I used to use a more general approach: develop an Application class with a property returning the main window reference.

Good luck,
--SA
arun_pk 8-Aug-12 15:42pm    
Just superb
it worked .. Thaaaaaaaaaaaaaaks a lot
Sergey Alexandrovich Kryukov 8-Aug-12 16:27pm    
My pleasure. And thank you for correct and interesting question.
Good luck, call again.
--SA
Christian Amado 8-Aug-12 16:04pm    
What piece of solution! Well done!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900