C# - WPF modal window - c#

Is it possible to switch or activate other windows when modal window is open?
Window1 window = new Window1();
window.ShowDialog();

If I understood you correctly you can do something like the following:
Window1 window = new Window1();
window.Activate();
This will activate window and bring it to the foreground.

I don't think it's possible to set the control back to parent form from a modal window(That's the point of using Modal Window). You can use non modal window.
window.Show();
If you use window.Activate(); it will not show the form.

Related

How to bring a new window open in front of Main Window in WPF after switching to any other application in MVVM architecture?

How can I resolve the following problem?
Run a WPF app, MainWindow is opened in front
New window is shown behind after opening any other application Main window is also disabled then have to press the ALT+Tab to take the new window and close it.
NewWindow newWindow = new NewWindow ();
newWindow.ShowDialog();
Set the Owner for your second window like this:
NewWindow newWindow = new NewWindow();
newWindow.Owner = this;
newWindow.ShowDialog();
I got it correct when I gave like this:
NewWindow newWindow = new NewWindow ();
newWindow.Owner= Application.Current.MainWindow;
newWindow.ShowDialog();

UserControl window won't close

I have a problem with one of my UserControl Windows.
I have a MainWindow and when a specific situatuin appears, another UserControl will open.
It has two Buttons which sends a command and after that it should be closed.
Right now it only opens the Window and sends the command, but doesn't close it afterwards.
I hope you can help me.
Code:
xaml:
C#:
Code to open the UserControl:
Window window = new Window();
window.Content = new MsgBox();
window.ShowDialog();
Button declaration:
public DelegateCommand OkBtn { get; set; }
Buttonfunction added to button:
OkBtn = new DelegateCommand<object>(OkBtnFkt);
In this Buttonfunction there should be something like: window.Close();
What I have tried:
Window.Close();
Sends the Window.close through the dispatcher to the UI
You could set the DialogResult of the window. What you actually have is a window with a UserControl inside. You could make a MsgBoxWindow to derive from Window. Place your control and a Close Button inside. Then if close button is clicked you can set the DialogResult.
https://msdn.microsoft.com/en-us/library/system.windows.window.dialogresult(v=vs.110).aspx
Anyway why don't you use MessageBox?
I found an answer.
This post had the answer for me :
http://www.codeproject.com/Questions/91746/Close-WPF-user-control-inside-WPF-form-application

Close another window in WPF

I'm building a multi-window app.
The problem I've got, I can't close "Window1" or "Window2" from button in "MainWindow".
All windows are in the same app.
Is there a way to kill another window?
Sure there is. If you have a reference to that Window, just call the Close() method.
Window window1 = new Window1();
window1.Show();
...
window1.Close();

Close Window after open another window?

I'm work on WPF project, let say I have two windows window1,window2, window1 call window2: in window1 code behind :
Window2 _window2 = new Window2();
_window2.ShowDialog();
What I want to know how I close first window (window1) after the secound window (window2) is opened ?
If "Window1" is not your main window you can just type this
Window2 _window2 = new Window2();
_window2.Show();
this.Close();
If your form is the main form than refer
(Windows Forms: Change application mainwindow at runtime)
Before you can close the main window (if it is), then you need to tell the app first which is the main window. Depending upon your shut down mode, if you close the main window, you shut down the app. You can set this using Application.Current.MainWindow property. See Here.
If you wish to close the first window after opening the second, you will need to hook up an event, or better still use a decoupled messaging system to inform the first window the second has opened.
Form1:
Window2 _window2 = new Window2();
this.Hide(); //Hides Form 1
_window2.ShowDialog();
Also see:
Close a Window from Another In Wpf
if you wanna have window1 closed.

Dialog in WPF dissapears when clicking on application icon in taskbar

I have a WPF application where i show a dialog with
nrDialog.WindowStartupLocation = WindowStartupLocation.CenterScreen;
bool? dialogResult = nrDialog.ShowDialog();
The dialog is a simple Window object with properties
Title="NewReportDialog" WindowStartupLocation="CenterOwner" ShowInTaskbar="False" ResizeMode="NoResize">
The windows works as normal modal dialog and locks mainwindow as it should , but when i click on the application icon on the taskbar, Windows 7, the modal dialog goes out of focus and the only way to get the dialog back in to focus is to use ALT-Tab
Anyone has a solution?
Try to set the Owner of the dialog Window to be the main application window. This should solve the issue.
Probably something like:
nrDialog.Owner = this;
Where this is the main window instance. You can replace it with something more relevant to your case.
You can get the main application window using: Application.Current.MainWindow

Categories