[WPF] 文件路径选择控件

1、创建一个WPF的自定义控件,SelectPathControl。

2、修改Style




    

4、添加对System.Windows.Forms.DLL的引用。

5、控件代码

using System.Windows;
using FolderBrowserDialog = System.Windows.Forms.FolderBrowserDialog;
using DialogResult = System.Windows.Forms.DialogResult;
using Button = System.Windows.Controls.Button;
using Control = System.Windows.Controls.Control;
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;
using SaveFileDialog = Microsoft.Win32.SaveFileDialog;

namespace SelectFile
{
    public enum SelectModeType
    {
        ///  选择文件
        /// 
        SelectFile,
        ///  选择文件夹
        /// 
        SelectFolder,
        ///  保存文件
        /// 
        SaveFile
    }

    [TemplatePart(Name = "SelectBtn", Type = typeof(Button))]
    public class SelectPathControl : Control
    {
        #region 属性

        public static readonly DependencyProperty PathProperty =
        DependencyProperty.Register("Path", typeof(string), typeof(SelectPathControl), new PropertyMetadata(string.Empty));
        ///  选择的路径
        /// 
        public string Path
        {
            get { return (string)GetValue(PathProperty); }
            set { SetValue(PathProperty, value); }
        }


        public static readonly DependencyProperty FilterProperty =
        DependencyProperty.Register("Filter", typeof(string), typeof(SelectPathControl), new PropertyMetadata("All|*.*"));
        ///  文件格式过滤器。
        /// 
        public string Filter
        {
            get { return (string)GetValue(FilterProperty); }
            set { SetValue(FilterProperty, value); }
        }

        public static readonly DependencyProperty SelectModeProperty =
            DependencyProperty.Register("SelectMode", typeof(SelectModeType), typeof(SelectPathControl), new PropertyMetadata(SelectModeType.SelectFile));
        ///  选择格式
        /// 
        public SelectModeType SelectMode
        {
            get { return (SelectModeType)GetValue(SelectModeProperty); }
            set { SetValue(SelectModeProperty, value); }
        }

        #endregion

        static SelectPathControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SelectPathControl), new FrameworkPropertyMetadata(typeof(SelectPathControl)));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var btn = GetTemplateChild("SelectBtn") as Button;
            if (btn != null)
            {
                btn.Click += (s, e) =>
                {
                    switch (SelectMode)
                    {
                        case SelectModeType.SelectFile: OpenSelectFileDialog(); break;
                        case SelectModeType.SelectFolder: OpenSelectFolderDialog(); break;
                        case SelectModeType.SaveFile: OpenSaveFileDialog(); break;
                    }
                };
            }
        }

        #region 按钮相应

        ///  设置保存的文件名称
        /// 
        private void OpenSaveFileDialog()
        {
            var dlg = new SaveFileDialog { Filter = Filter, FileName = Path };
            var res = dlg.ShowDialog();
            if (res != true) return;
            Path = dlg.FileName;
        }

        ///  选择文件
        /// 
        private void OpenSelectFileDialog()
        {
            var dlg = new OpenFileDialog { Filter = Filter, FileName = Path };
            var res = dlg.ShowDialog();
            if (res != true) return;
            Path = dlg.FileName;
        }

        ///  选择文件夹
        /// 
        private void OpenSelectFolderDialog()
        {
            var dlg = new FolderBrowserDialog { SelectedPath = Path };
            var res = dlg.ShowDialog() == DialogResult.OK;
            if (!res) return;
            Path = dlg.SelectedPath;
        }

        #endregion
    }
}

6、前台调用


链接:源码下载

你可能感兴趣的:(WPF)