I'm trying to use the FolderBrowserDialog from my WPF application - nothing fancy. I don't much care that it has the Windows Forms look to it.
However, when I call ShowDialog, I want to pass the owner window which is an IWin32Window. How do I get this from my WPF control?
Actually, does it matter? If I run this code and use the ShowDialog overload with no parameters it works fine. Under what circumstances do I need to pass the owner window?
Thanks,
Craig
Unlike other Windows Forms controls, a FolderBrowserDialog does not have or need visual properties like others. To create a FolderBrowserDialog control at design-time, you simply drag and drop a FolderBrowserDialog control from Toolbox to a Form in Visual Studio.
WPF, stands for Windows Presentation Foundation is a development framework and a sub-system of . NET Framework. WPF is used to build Windows client applications that run on Windows operating system. WPF uses XAML as its frontend language and C# as its backend languages.
And here's my final version.
public static class MyWpfExtensions { public static System.Windows.Forms.IWin32Window GetIWin32Window(this System.Windows.Media.Visual visual) { var source = System.Windows.PresentationSource.FromVisual(visual) as System.Windows.Interop.HwndSource; System.Windows.Forms.IWin32Window win = new OldWindow(source.Handle); return win; } private class OldWindow : System.Windows.Forms.IWin32Window { private readonly System.IntPtr _handle; public OldWindow(System.IntPtr handle) { _handle = handle; } #region IWin32Window Members System.IntPtr System.Windows.Forms.IWin32Window.Handle { get { return _handle; } } #endregion } }
And to actually use it:
var dlg = new FolderBrowserDialog(); System.Windows.Forms.DialogResult result = dlg.ShowDialog(this.GetIWin32Window());
If you specify Owner, you will get a Modal dialog over the specified WPF window.
To get WinForms compatible Win32 window create a class implements IWin32Window like this
public class OldWindow : System.Windows.Forms.IWin32Window { IntPtr _handle; public OldWindow(IntPtr handle) { _handle = handle; } #region IWin32Window Members IntPtr System.Windows.Forms.IWin32Window.Handle { get { return _handle; } } #endregion }
And use an instance of this class at your WinForms
IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle; // 'this' means WPF Window folderBrowserDialog.ShowDialog(new OldWindow(mainWindowPtr));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With