How to use a FolderBrowserDialog from a WPF application

前端 未结 10 2084
青春惊慌失措
青春惊慌失措 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:10

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

提交回复
热议问题