WPF equivalent of Form.ShowDialog(IWin32Window) - c#

In WPF we have Window.ShowDialog() which allows showing a modal dialog box.
In WinForms there is similar functionality but it also has an overload Form.ShowDialog(IWin32Window) which allows an IWin32Window owner to be passed in. That way the new dialog is not modal, and always maintains a z-order directly above its owner.
How would I get this same functionality using WPF?

Use the Owner property on a Window.

To expand on #Jonathan.Peppers's answer:
Say you had a Window you named FooWindow, and in BarWindow.cs you wanted to create and execute an instance. You can create a modal version of FooWindow as simple as this:
new FooWindow(){ Owner = this }.ShowDialog();
That would assume you didn't need a reference to you instance, obviously, but you get the idea?

Related

Prism WPF: How to Open A New Window/Dialog

When using code behind, the code looks like this:
AnotherWindow x = new AnotherWindow();
x.Show() ;
// or x.ShowDialog()
But how can I achieve this using MVVM? Specifically Prism?
In case you need to build a dialog for asking user login input or progressing dialog, MahApps.Metro can be a useful toolkit as it provides you with some built-in dialog UI/functionalities with MVVM pattern. For more information, check some examples here:
https://mahapps.com/controls/dialogs.html
In Prism, there's the InteractionRequest for short-lived dialogs. If you're looking for a long living dialog, like a second application window or shell, you're stuck with new Window ... Show.
To make your dialog service mvvm-friendly, you should hide it behind an interface and make it as generic as possible. Using view model first here eliminates the need to specify a window type, because you can provide a default window that just contains one large ContentControl, and the view can be mapped as DataTemplate.

Using Owner Handle in WPF window Show method

In Winforms we have Show(IWin32Window owner) method to Show a form based on the owner handle passed to the Show method.
I want to have similar behavior in WPF Window. But WPF window doesn't have Show method which accepts an owner handle.
Another alternative which I found is by setting window.Owner to the owner window and then use window.Show(). But by doing this we can assign only a WPF window as the owner.
In my case the owner can be a winform or a WPF window. How can we achieve this?
Setting the Owner property of the window is the correct way of creating a relationship between two WPF windows (as per Remarks section of the Window.Owner property MSDN page).
However, if you want to set the owner to a Winforms Form, you have to use WindowInteropHelper class:
WindowInteropHelper helper = new WindowInteropHelper(wpfWindow);
helper.Owner = form.Handle;

How to get result from custom WPF window to parent window in UserControl

How to use WPF window as a messagebox? here is how I was able to get messagebox. Now I want it to return back certain value in the userControl. Any help?
as #SLaks says, use the DialogReslult... if that is not enough and you are using an MVVM model, then you could use your data model: set the DataContext of the child window to your data model instance then you can bind the contorls in your child window to any data member on your model--typically you would set the DataContext to the DataContext of the parent window...
protected popMyWindow()
{
MyChildWindow cw = new MyChildWindow();
cw.DataContext = this.DataContext();
// show the window...
}
Set the window's DialogResult in the window itself before closing.
The value you set will be returned by ShowDialog()
If you want to return more than a bool?, create a wrapper method that calls ShowDialog() and returns whatever you want.
The WPF solution for these problems is the 'Page Function'.
PageFunction is a new term defined in WPF. It enables the user to navigate to a specific page and perform a task, then navigate back to the caller page with the result. It behaves just like Modal Dialogbox with the difference that PageFunction won’t be displayed as s pop-up, instead it is displayed in the same page as the caller.
Source: http://blogs.msdn.com/b/marthami/archive/2007/10/02/how-to-use-pagefunction-to-create-dialog-behavior-in-wpf.aspx
It differs from the pattern of wrapping the ShowDialog in that the page is navigated to, and more importantly, it is already strongly typed within the WPF plumbing and does not require you to develop a new class to do the same thing.
There is an explanatory StackOverflow thread here...
WPF - PageFunctions. Why are they needed?

WPF OpenFileDialog with Win32 parent window; window is not regaining focus when closed

I have a c++ windows app calling into a c++/cli library, which is calling into a managed library. The managed library is calling OpenFileDialog.Show with a WPF window parent which is owned by the Win32 window. I haven't found a better way to do this, and all the resources I've read here and searching google and social.msdn recommend doing what I'm doing.
The dialog opens just fine, but when I hit the cancel button, for instance, the app loses focus completely. I'm not sure why it's happening, but I can't seem to make it stop. I've tried a number of different things to no avail.
If I just launch the OpenFileDialog without creating a WPF Window, I don't see the problem.
If I don't set the owner of the WPF Window, I don't see the problem. If I call OpenFileDialog.Show and don't pass the parent, but still create the WPF Window and set its owner, I still see the problem.
I am able to hack it to set the parent app window to foreground after it loses focus, but I would like to not have to.
I have uploaded a small example solution for my scenario that illustrates the problem:
http://dl.dropbox.com/u/26054523/MixedExample.zip
Any help would be appreciated.
Have you tried inverting the hosting scenario? Right now it sounds like you're going unmanaged->bridge->managed->WPF->Winforms. Maybe you could go ...managed->WinForms->WPF using ElementHost http://msdn.microsoft.com/en-us/library/ms742215.aspx
In that way, the WPF window would just be a child control of the WinForms app and that might work out better for focus switches. WinForms controls are not really meant to work directly with WPF apps so well, two different UI threading setups are being used as you've noted.
I know that this is an old post but I think that this is a common problem and I have a good answer. If you have a Win32 window parent window called ParentWindow and a WPF child window called WPFChild you can do this:
using System.Windows.Interop;
void OpenWindow()
{
WPFChildWindow WPFChild = new WPFChildWindow();
WindowInteropHelper helper = new WindowInteropHelper(WPFChild)
{
Owner = new NativeWindowWrapper(ParentWindow.Hwnd).Handle
};
bool? ret = _stepsForm.ShowDialog();
}
This will cause the child window to remain on top of the parent and function as a dialog. Keep in mind that the WPF window does not return a DialogResult but rather a nullable bool.
NativeWindow wrapper is a simple class that takes casts an int as an IntPtr. It's actually from a .net Excel ref edit project located here: How to code a .NET RefEdit ControlT

C#/WPF, how to make a window (created with Window.ShowDialog()) title bar blink when clicking its parent window (like MessageBox does)?

I'm trying to create a custom MessageBox by using a WPF Window that is called with ShowDialog().
So far, I've managed to implement everything, except for one thing.
As you know, when you use MessageBox.Show("text"); you cannot set the focus or click the parent window (the one that called the MessageBox). If you do try to click the parent window, the MessageBox will blink briefly in order to alert you that you must close if first.
Windows created with Window.ShowDialog();, however, do not show that behavior. In fact, while you cannot set the focus to the parent window, the child (called with ShowDialog()) will never blink briefly.
My question is, is there any way to implement that in WPF? I've been searching for an answer but I must admit, I am stumped.
Thanks everyone!
You need to set the Owner of the modal window correctly, e.g. using the following code from within the owning window:
Window win = new SomeModalWindow();
win.Owner = this;
win.ShowDialog();
You would have to set Owner property of the child Window to the parent Window. See the MSDN Documentation here.

Categories

Resources