WPF window in front of another WPF window - wpf

I have a WPF-window, in this you can click a button to see a new window. Now I want to disable, that you can click the main-window. It should be like a MessageBox.

You want to use dialog.ShowDialog().
Modeless dialogs (via dialog.Show) are ones that you can interact with the background window. Modal dialogs (via dialog.ShowDialog() are ones that don't allow you to interact with the parent window.

You're looking for Window.ShowDialog()

Try this:
dialogYouNeedToShow.Owner = App.Current.MainWindow;
dialogYouNeedToShow.ShowDialog();
ShowDialog will always show your dialog as a modal one, so you won't have access to background form.

Do you mean
MessageBox.Show()
or
myWindowName.ShowDialog()
?

Related

WPF - Focus second window by clicking a button

I'm creating a WPF application and I have 3 Windows (MainWindow, Window2 and Window3). When I start the project Window2 loads right after my MainWindow is loaded. And when I click a Button, Window3 opens
Window3 w3 = new Window3(this, this.window2);
w3.ShowDialog();
but I'm not able to do things (click controls etc.) in Window2, I know that ShowDialog() method is disabling other open Windows but is there a possibility to access using those controls in Window2. How could I get access to Window2's functionalities when Window3 is open?
PS.(Sorry for asking a stupid question but I'm beginner in WPF)!
You can access System.Windows.Application static class's Windows collection to have list of all currently opened windows. Then you can iterate the collection and find your window by type or some other criteria. One way would be:
var window3 = Application.Current.Windows.OfType<Window3>().FirstOrDefault();
Edit:
In comments to the question you stated that you were opening window using w3.Show() method. That opens window in non modal way. While after editing the question w3.ShowDialog() opens the window in modal way.
You can learn more about modal windows at MSDN
A modal dialog box is displayed by a function when the function needs additional data from a user to continue. Because the function depends on the modal dialog box to gather data, the modal dialog box also prevents a user from activating other windows in the application while it remains open.
So to be able to switch focus between windows they should be non modal and opened using Show method instead of ShowDialog.

Extjs 4.1 window.close() does not destroy it

I create an object of window, and put tabpanel in this window. but window.close() method does not destroy window. When I click button to open window again it display 2 different tabpanel, one from old window and another from new window and it crash my user interface.
I also called listener "beforeclose" and in this method destroy tabpanel but that doest not work.
What can be done?
Are you sure that closeAction option is set to destroy?
Of course it is a good idea to show simplified sources, so we can understand where the problem exists.
Add closeAction:'destroy' to your window. It should work.

When Modal Dialog is shown in WPF and a user clicks on the parent window, make the modal dialog flash, blink or shake

New Thought, Maybe I am looking at this totally incorrectly. So Here is exactly what I am trying to do in case there is another option I am not aware of.
I have a WPF app, the main window shows a smaller dialog window using ShowDialog(), when a user clicks on the parent window that showed the dialog, I need to make the dialog window, flash, shake or blink.
AresAvatar posted a link that looks like it might be able to use, but is there another option I am not aware of?
My original Question.
Mouse click event when Modal window's parent is clicked in WPF app?
I have a wpf app that shows a modal window using ShowDialog().
I would like to fire an event when the user tries to click the parent window that is now disabled.
Is it possible on the parent to receive a click event when it has shown a modal window?
When I attempted this using an interaction trigger, the events never fired on parent window.
Otherwise, what suggestions / options are there.
Thanks
No WPF events are sent under these conditions. The only Windows message I can see that gets sent is WM_WINDOWPOSCHANGING. You could check for that message, and check if the window was disabled when it occurred. Here's a good article on checking WM_WINDOWPOSCHANGING.
Edit: that link seems to be dead. Here's an example on StackOverflow of checking window messages.
I know this is an old question but I'll post my solution in case any one needs it.
Set the dialog.owner prior to calling ShowDialog().
var dialog = new DialogWindow();
dialog.owner = MainWindow;
dialog.ShowDialog();
The result is that clicking on the main window, brings the dialog window to the front and makes the dialog window flash.

WPF Popup Activation and Deactivation

Is there a possibility to detect if a Popup is activated or deactivated? Such as the Window-events Window.Activated and Window.Deactivated.
I want to detect if the user has clicked outside of the popup (I can not use the Popup.StaysOpen=false option).
Update
I have forgotten to remark, that I dont want to add any handler or code to a parent-control of the popup.
Capture mouse clicks for your main application window (MousePreview type event) and for popup - when user clicks into popup, main window event doesn't fire and vice versa.
Sounds like you could just use the Popup.Opened and Popup.Closed events.

WinForms modal windows alt+tab problem

Suppose multiple Modal Windows shown above each other.
All of those have ShowInTaskbar = false, which means that in the TaskBar you only see the MainForm and all Modal Windows are hidden.
Now you press ALT+ TAB and the most upper modal Windows disappears. But you cannot get it back in front.
How should be this done correctly in your opinion?
If a modal window is getting stuck behind the main form, it sounds like you are not setting its owner. When you call showDialog(), you need to pass in the main form like this:
modalWin.showDialog(mainForm);
Any time you call showDialog(), and your program has another form that should be underneath, it is best to pass it as the owner. If you show modal window when there is already a modal window up, then pass the first modal window as the owner.
OK Just to complete it:
This is how to set an Owner to be a Winform for a Winform:
form.ShowDialog(ownerInstance);
This is how to set an Owner to be a Winform for a WPF Window:
MyWpfDialog dialog = new MyWpfDialog();
new System.Windows.Interop.WindowInteropHelper(dialog).Owner = ownerInstance.Handle;
dialog.ShowDialog();
This is how to set an Ownder to be a Wpf Window for a Wpf Window:
.Owner = Window.GetWindow(ownerInstance)

Resources