[WPF] 文件路徑選擇控件

1、創建一個WPF的自定義控件,SelectPathControl。

2、修改Style

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:SelectFile">


    <Style TargetType="{x:Type local:SelectPathControl}">
        <Setter Property="Height" Value="25"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:SelectPathControl}">
                    <DockPanel>
                        <Button x:Name="SelectBtn" DockPanel.Dock="Right"  Height="{TemplateBinding Height}" Width="{Binding RelativeSource={RelativeSource Self},Path=Height}" Content="..."/>
                        <TextBox Text="{TemplateBinding Path}" Margin="0,0,5,0" MinWidth="80"/>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

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
    {
        /// <summary> 選擇文件
        /// </summary>
        SelectFile,
        /// <summary> 選擇文件夾
        /// </summary>
        SelectFolder,
        /// <summary> 保存文件
        /// </summary>
        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));
        /// <summary> 選擇的路徑
        /// </summary>
        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|*.*"));
        /// <summary> 文件格式過濾器。
        /// </summary>
        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));
        /// <summary> 選擇格式
        /// </summary>
        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 按鈕相應

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

        /// <summary> 選擇文件
        /// </summary>
        private void OpenSelectFileDialog()
        {
            var dlg = new OpenFileDialog { Filter = Filter, FileName = Path };
            var res = dlg.ShowDialog();
            if (res != true) return;
            Path = dlg.FileName;
        }

        /// <summary> 選擇文件夾
        /// </summary>
        private void OpenSelectFolderDialog()
        {
            var dlg = new FolderBrowserDialog { SelectedPath = Path };
            var res = dlg.ShowDialog() == DialogResult.OK;
            if (!res) return;
            Path = dlg.SelectedPath;
        }

        #endregion
    }
}

6、前臺調用

<selectFile:SelectPathControl SelectMode="SaveFile" Filter="BMP|*.bmp|GIF|*.gif|JPG|*.jpg;*.jpeg"/>
<selectFile:SelectPathControl SelectMode="SelectFolder"/>

鏈接:源碼下載
發佈了40 篇原創文章 · 獲贊 5 · 訪問量 37萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章