How to use a FolderBrowserDialog from a WPF application

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

    OK, figured it out now - thanks to Jobi whose answer was close, but not quite.

    From a WPF application, here's my code that works:

    First a helper class:

    private 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
    }
    

    Then, to use this:

        System.Windows.Forms.FolderBrowserDialog dlg = new FolderBrowserDialog();
        HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
        System.Windows.Forms.IWin32Window win = new OldWindow(source.Handle);
        System.Windows.Forms.DialogResult result = dlg.ShowDialog(win);
    

    I'm sure I can wrap this up better, but basically it works. Yay! :-)

    0 讨论(0)
  • 2020-12-02 11:03

    VB.net translation

    Module MyWpfExtensions
    
    Public Function GetIWin32Window(this As Object, visual As System.Windows.Media.Visual) As System.Windows.Forms.IWin32Window
    
        Dim source As System.Windows.Interop.HwndSource = System.Windows.PresentationSource.FromVisual(Visual)
        Dim win As System.Windows.Forms.IWin32Window = New OldWindow(source.Handle)
        Return win
    End Function
    
    Private Class OldWindow
        Implements System.Windows.Forms.IWin32Window
    
        Public Sub New(handle As System.IntPtr)
            _handle = handle
        End Sub
    
    
        Dim _handle As System.IntPtr
        Public ReadOnly Property Handle As IntPtr Implements Forms.IWin32Window.Handle
            Get
    
            End Get
        End Property
    
    
    End Class
    
    End Module
    
    0 讨论(0)
  • 2020-12-02 11:08
    //add a reference to System.Windows.Forms.dll
    
    public partial class MainWindow : Window, System.Windows.Forms.IWin32Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void button_Click(object sender, RoutedEventArgs e)
        {
            var fbd = new FolderBrowserDialog();
            fbd.ShowDialog(this);
        }
    
        IntPtr System.Windows.Forms.IWin32Window.Handle
        {
            get
            {
                return ((HwndSource)PresentationSource.FromVisual(this)).Handle;
            }
        }
    }
    
    0 讨论(0)
  • 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));
    
    0 讨论(0)
提交回复
热议问题