Select folder dialog WPF

后端 未结 10 650
暗喜
暗喜 2020-11-28 19:32

I develop a WPF4 application and in my app I need to let the user select a folder where the application will store something (files, generated reports etc.).

My requ

相关标签:
10条回答
  • 2020-11-28 20:04

    Microsoft.Win32.OpenFileDialog is the standard dialog that any application on Windows uses. Your user won't be surprised by its appearance when you use WPF in .NET 4.0

    The dialog was altered in Vista. WPF in .NET 3.0 and 3.5 still used the legacy dialog but that was fixed in .NET 4.0. I can only guess that you started this thread because you are seeing that old dialog. Which probably means you're actually running a program that is targeting 3.5. Yes, the Winforms wrapper did get the upgrade and shows the Vista version. System.Windows.Forms.OpenFileDialog class, you'll need to add a reference to System.Windows.Forms.

    0 讨论(0)
  • 2020-11-28 20:09

    MVVM + WinForms FolderBrowserDialog as behavior

    public class FolderDialogBehavior : Behavior<Button>
    {
        public string SetterName { get; set; }
    
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.Click += OnClick;
        }
    
        protected override void OnDetaching()
        {
            AssociatedObject.Click -= OnClick;
        }
    
        private void OnClick(object sender, RoutedEventArgs e)
        {
            var dialog = new FolderBrowserDialog();
            var result = dialog.ShowDialog();
            if (result == DialogResult.OK && AssociatedObject.DataContext != null)
            {
                var propertyInfo = AssociatedObject.DataContext.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
                .Where(p => p.CanRead && p.CanWrite)
                .Where(p => p.Name.Equals(SetterName))
                .First();
    
                propertyInfo.SetValue(AssociatedObject.DataContext, dialog.SelectedPath, null);
            }
        }
    }
    

    Usage

         <Button Grid.Column="3" Content="...">
                <Interactivity:Interaction.Behaviors>
                    <Behavior:FolderDialogBehavior SetterName="SomeFolderPathPropertyName"/>
                </Interactivity:Interaction.Behaviors>
         </Button>
    

    Blogpost: http://kostylizm.blogspot.ru/2014/03/wpf-mvvm-and-winforms-folder-dialog-how.html

    0 讨论(0)
  • 2020-11-28 20:13

    I wrote about it on my blog a long time ago, WPF's support for common file dialogs is really bad (or at least is was in 3.5 I didn't check in version 4) - but it's easy to work around it.

    You need to add the correct manifest to your application - that will give you a modern style message boxes and folder browser (WinForms FolderBrowserDialog) but not WPF file open/save dialogs, this is described in those 3 posts (if you don't care about the explanation and only want the solution go directly to the 3rd):

    • Why am I Getting Old Style File Dialogs and Message Boxes with WPF
    • Will Setting a Manifest Solve My WPF Message Box Style Problems?
    • The Application Manifest Needed for XP and Vista Style File Dialogs and Message Boxes with WPF

    Fortunately, the open/save dialogs are very thin wrappers around the Win32 API that is easy to call with the right flags to get the Vista/7 style (after setting the manifest)

    • Vista style open and save dialogs with WPF (without using the Vista bridge sample)
    0 讨论(0)
  • 2020-11-28 20:15

    The Ookii Dialogs for WPF has a VistaFolderBrowserDialog class that provides a complete implementation of a folder browser dialog for WPF.

    https://github.com/augustoproiete/ookii-dialogs-wpf

    There's also a version that works with Windows Forms.

    0 讨论(0)
提交回复
热议问题