Displaying Page Over MainWindow in WPF - wpf

I have a main window in Which I have a button. When i click that button i want to show a dialog box which i have made using a page. I am using
Page1 P = new Page(); //Page which i want to show over my main window
this.P.Visibility = Visibility.Visible;
when i used Window to make dialog box it is working fine but with pages there is no Show property associated. Does anyone know Of any method to display page over Mainwindow.

A Page cannot be shown as a window. For this you need to use Window, both can host the same XAML content, you just need to use the right container!
See this question:
Page vs Window in WPF?

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.

WPF: Adorner + Popup = Modal Popup

So here's the idea of it...
I have an application that I am designing with WPF. It has a Main Window with a frame inside to navigate through pages. The navigation aspect is done through the window (kind of like a template with a footer that has the navigation controls) and the pages have the individual content. On certain pages I would like to have an ADD (+) button that causes a popup that has a form inside to add personal information to the database (First name, last name, address, .etc) While this popup is open I would not like them to be able to access any other part of the application until they press OK or CANCEL. I'd like to be able to call this popup from the individual pages unless there is another workaround.
Any ideas or assistance would be appreciated!
some wpf popups Here and Here.

Displaying a modal dialog (ChildWindow) from a user control

I have a user control that has a Grid as a main container. This control needs to display a modal dialog. However, when I show the dialog (implements ChildWindow) from the control, nothing happens, no errors and no dialog.
MyDialog dialog = new MyDialog();
dialog.Show();
If ChildWindow is something that can only be displayed from the main page and not user control, what's my alternative?
Solved the problem by creating a user control and putting it in the same grid location as the original control with Visibility:Collapsed. Every time I'd need a modal dialog, I'd disable the user control that is visible and set Visibility:Visible on the modal dialog user control.
This blog post helped a great deal: Silverlight Tutorial Part 6: Using User Controls to Implement Master/Detail Scenarios

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)

Popup window and context menu

I am using the ToolStripDropDown to host the user control as the pop-up window. The problem is when a context menu strip is displayed from within this pop-up window, the pop-up itself closes in the moment the context menu opens.
I have tried to subclass the ContextMenuStrip and added WS_EX_NOACTIVATE to CreateParams but nothing changed. First I thought that there is no way to do this since it is common behavior but then I tried to put a TextBox class onto the pop-up user control and invoke the Edit control context menu - and the parent pop-up window did not close.
What am I missing?
Had a similary Problem. On my UserControll was a toolstrip. When I pressed the toolsstripdropdownbutton the dropdown was shown but the popup disapeared.
The reason was that popup.Autoclose was true. After Setting to false the Popup is not closed any more.
ToolStripDropDown popup = new ToolStripDropDown();
popup.AutoClose = false; //Set to FALSE
popup.Margin = Padding.Empty;
popup.Padding = Padding.Empty;
ToolStripControlHost host = new ToolStripControlHost(userControl1);
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
popup.Items.Add(host);
popup.Show(button1, new Point(100,100));
Actual Solution should be the one in Martin's final comment:
Use ContextMenu Instead of ContextMenuStrip
That one worked for me, and the ToolStripDropDown no longer closes by itself when right clicking one of its content controls, like it should. We still need it to AutoClose, disabling AutoClose on ToolStripDropDown will do bad things, it is supposed to close on losing focus. Example: open any other app window, and the ToolStripDropDown will continue to appear on top

Resources