Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: XAML WPF C#4.0
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 8-Aug-12 6:43am
Wes Aday at 8-Aug-12 11:46am
Sounds like you are not really showing the dialog as modal.
Sergey Alexandrovich Kryukov at 8-Aug-12 13:50pm
I suspect that, too.
arun_pk at 8-Aug-12 11:48am
private void button1_Click(object sender, RoutedEventArgs e)
Window1 aa = new Window1();
i am doing it in this way
Wes Aday at 8-Aug-12 11:57am
That code behaves exactly as it is supposed to on my system.
Sergey Alexandrovich Kryukov at 8-Aug-12 13:52pm
OP asked a right question below...
Sergey Alexandrovich Kryukov at 8-Aug-12 14:22pm
Actually, I hope I found the resolution, please see.
arun_pk at 8-Aug-12 12:02pm
can u press windows + D
then click on your application..
please lemme know how it behaves
Sergey Alexandrovich Kryukov at 8-Aug-12 13:52pm
You should have better reply to the Wes's comment, to have them receive a notification.
Wes Aday at 8-Aug-12 14:14pm
It behaves exactly like a modal dialog.
Sergey Alexandrovich Kryukov at 8-Aug-12 15:25pm
Did you see it? OP confirmed that my solution works!
A modal window does not become owned by the main window automatically, one should assign the owner, only then it works correctly. It was a good question, up-voted.
arun_pk at 8-Aug-12 14:14pm
open the application.. click on button which open the child modal window
click windows key + d .. Now desktop is shown
again click back the application from taskbar ..
my main application is shown.
the child modal window is not focused
Sergey Alexandrovich Kryukov at 8-Aug-12 14:24pm
Yes, yes, I understand it. You question (considering comments) is correctly formulated and shows your attention to important but often missing detail, so I voted 5 for the question (a very rare case these days!). I also provided an answer which should resolve the problem. Please try it and report back, accept the answer formally in case of success.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:[^].
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:[^].
Now, let's see at the documentation on ShowDialog:[^].
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.
arun_pk at 8-Aug-12 15:13pm
Thanks 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 at 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,
arun_pk at 8-Aug-12 15:42pm
Just superb
it worked .. Thaaaaaaaaaaaaaaks a lot
Sergey Alexandrovich Kryukov at 8-Aug-12 16:27pm
My pleasure. And thank you for correct and interesting question.
Good luck, call again.
Christian Amado at 8-Aug-12 16:04pm
What piece of solution! Well done!
Sergey Alexandrovich Kryukov at 8-Aug-12 16:28pm
Thank you, Christian.
Volynsky Alex at 8-Aug-12 17:30pm
Sergey Alexandrovich it is good answer!
Sergey Alexandrovich Kryukov at 8-Aug-12 17:42pm
Thank you very much, Alex.

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

  Print Answers RSS
0 Maciej Los 495
1 OriginalGriff 400
2 Sergey Alexandrovich Kryukov 394
3 CHill60 320
4 Sascha Lefévre 269
0 Sergey Alexandrovich Kryukov 10,017
1 OriginalGriff 9,495
2 Peter Leow 5,241
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 3,076

Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 8 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100