Closing a Window in WPF (When not in that Window) - wpf

I have an application which starts with a simple start screen allowing the user to select either New or Open a project. When selecting New I have a new window displayed which is a wizard that collects data to be passed to the Main window.
I create a new Window for the Main window and show that.
Then I close the wizard easily enough with this.close();
But how do I close the initial window which is the Startup URI window?

Application.Current.MainWindow.Close();

Pass the Startup window to the main window as a constructor parameter or property, then call Close() on it.

Related

How can I iterate to the window control from a form

How can I iterate to the window control from a form in the below hiearchy?
Windows.Forms--> WPF UserControl --> WPF Window(Using ShowDialog).
LogicalTreeHelper.GetChildren does not catch the Window object.
A window that is opened by calling Show 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. FRom here - Window.Show Method()
But you can't set Owner for your WPFWindow because 'Owner' property can get only WPFWindow but you have as owner WinForms window. Maybe you can just save WindowControl as instance variable in you class so then you can get it any time you want.

How to restore a window while it is minimized in system tray?

I have a wpf form which has one main window. when i click a button in main window it i will show a child window. I tried to restore the child window but couldn't. how to restore while it is minimized in system tray?
Something like this should work
if (this.MainWindow.WindowState == WindowState.Minimized)
{
this.MainWindow.WindowState = WindowState.Normal;
}
//bring window to front of all windows
this.MainWindow.Activate();
this.MainWindow should be the reference to the window you want to maximize or bring to normal state.
Regards,

How To Make WPF ShowDialog() Block Parent Window

WPF 4.5
I've got an app where I show a modal Window from my app's main Window by instantiating it, setting its owner to be the main Window, and then calling ShowDialog(). The modal Window has "Topmost=true". The main Window does not.
When I run my app the modal Window shows in front of the main Window, and it is kept in front of the main Window just as expected. However, I can simply click on the main Window behind the modal Window to Activate the main Window and manipulate it...I can even close it!
In my humble opinion this is definitely not the desired behavior for an application with a modal Window. I'm confused as to why WPF handles it this way. More important, I need a solution that will keep the modal Window in front while also blocking access to the main Window behind it (isn't that supposed to be a basic function of a modal Window?)
I believe this desired behavior has always been the default behavior when using ShowDialog in WinForms (way back in the day.) What am I missing here, and how can I get this working with WPF?
You have to set the owner window before ShowDialog :
modalWindow.Owner = RootWindow;

Application.Current.Activated doesn't fire while .ShowDialog() active

I am using Application.Current.Activated to bring all windows to the front when any one is clicked. This is working great, but doesn't work when I have a modal dialog open using .ShowDialog()
Clicks on other windows of the application (besides the dialog) do not result in the application being activated. The default behavior is to do nothing. (The clicks appear to be entirely IGNORED!)
How can I make it so that when any window of the application is clicked, the open dialog is brought to the front? (The expected behavior based on how pretty much every application works)
YES: I did set the owner of the dialog to my MainWindow, and clicking on the main window produces the desired result, but clicking any other window does nothing.
Code:
Create a new WPF project and add two windows.
Then put a button on MainWindow.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Window1 foo = new Window1();
foo.Show();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window2 modal = new Window2();
modal.Owner = this;
modal.ShowDialog();
}
}
To see what I'm talking about, click the button to open the modal dialog.
The desired behavior is what happens when MainWindow is clicked.
I want the same to happen when Window1 is clicked.
AFAIK, a modal dialog (which .ShowDialog() triggers) blocks all other windows thereby Activation events.
As per the MSDN:
When a Window class is instantiated, it is not visible by default. ShowDialog shows the window, disables all other windows in the application, and returns only when the window is closed. This type of window is known as a modal window.
http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog(v=vs.110).aspx
This may be an ugly hack, but maybe you can accomplish what you need to by creating your modal window on a separate thread thereby letting your main application continue to process activation (and other) events? You would need a way to prevent 2 of that 2nd modal dialog from opening though.

Get parent window from popup element

I have popup on my current application. There is one image inside popup control. Now when I click on that image, I want to have object of main app window.
When I write this, it gives me null.
Window window = VisualTreeHelpers.FindVisualParent<Window>(Image);
And When i write this, it gives me object of POPUP windo, not application window.
window = Window.GetWindow(Image);
How would I get main application window from Popup Image control ?
Application.Current.MainWindow
MainWindow is automatically set with a reference to the first Window
object to be instantiated in the AppDomain.
You can specify a different main window by setting MainWindow
assigning another Windows object to the MainWindow property.
Link to MSDN page is here.

Resources