如何使用 WPF 应用程序中的 FolderBrowserDialog [英] How to use a FolderBrowserDialog from a WPF application
本文介绍了如何使用 WPF 应用程序中的 FolderBrowserDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用我的 WPF 应用程序中的 FolderBrowserDialog - 没什么特别的.我不太关心它是否具有 Windows 窗体外观.
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.
然而,当我调用 ShowDialog 时,我想传递一个 IWin32Window 的所有者窗口.如何从我的 WPF 控件中获取此信息?
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?
其实,重要吗?如果我运行此代码并使用不带参数的 ShowDialog 重载,则它可以正常工作.什么情况下需要通过所有者窗口?
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?
谢谢,
克雷格
推荐答案
这是我的最终版本.
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
}
}
并实际使用它:
var dlg = new FolderBrowserDialog();
System.Windows.Forms.DialogResult result = dlg.ShowDialog(this.GetIWin32Window());
这篇关于如何使用 WPF 应用程序中的 FolderBrowserDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文