How to use a FolderBrowserDialog from a WPF application

前端 未结 10 2096
青春惊慌失措
青春惊慌失措 2020-12-02 10:29

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

10条回答
  •  我在风中等你
    2020-12-02 11:01

    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());
    

提交回复
热议问题