using Microsoft.Win32; namespace WPFluent.Controls; public class ChooseBox : System.Windows.Controls.TextBox { private System.Windows.Controls.Button PART_ChooseButton; static ChooseBox() { DefaultStyleKeyProperty.OverrideMetadata(typeof(ChooseBox), new FrameworkPropertyMetadata(typeof(ChooseBox))); } public string PlaceholderText { get => (string)GetValue(PlaceholderTextProperty); set => SetValue(PlaceholderTextProperty, value); } public static readonly DependencyProperty PlaceholderTextProperty = DependencyProperty.Register("PlaceholderText", typeof(string), typeof(ChooseBox), new PropertyMetadata(string.Empty)); #region Event Implement Function private void PART_ChooseButton_Click(object sender, RoutedEventArgs e) { switch (ChooseType) { case ChooseType.SingleFile: OpenFileDialog openFileDialog = new() { Multiselect = false, //"文本文件|*.*|C#文件|*.cs|所有文件|*.*" Filter = Filter }; if (openFileDialog.ShowDialog() == true) this.Text = openFileDialog.FileName; break; case ChooseType.MultiFiles: OpenFileDialog multiFileDialog = new() { Multiselect = true, Filter = Filter }; if (multiFileDialog.ShowDialog() == true) { string fileName = string.Empty; foreach (var item in multiFileDialog.FileNames) { fileName += $"{item};"; } this.Text = fileName.TrimEnd(';'); } break; case ChooseType.Folder: var folderDialog = new VistaFolderBrowserDialog(); if (folderDialog.ShowDialog() == true) this.Text = folderDialog.SelectedPath; break; case ChooseType.SaveFile: var fileDialog = new SaveFileDialog { Filter = Filter, //设置文件类型 FileName = DefaultFileName, //设置默认文件名 DefaultExt = DefaultExt, //设置默认格式(可以不设) AddExtension = true //设置自动在文件名中添加扩展名 }; if (fileDialog.ShowDialog() == true) this.Text = fileDialog.FileName; break; } } #endregion #region Override public override void OnApplyTemplate() { base.OnApplyTemplate(); PART_ChooseButton = this.GetTemplateChild("PART_ChooseButton") as System.Windows.Controls.Button; if (PART_ChooseButton != null) { PART_ChooseButton.Click += PART_ChooseButton_Click; } } #endregion #region ChooseButtonStyle /// /// 获取或者设置选择按钮的样式 /// public Style ChooseButtonStyle { get => (Style)GetValue(ChooseButtonStyleProperty); set => SetValue(ChooseButtonStyleProperty, value); } public static readonly DependencyProperty ChooseButtonStyleProperty = DependencyProperty.Register( nameof(ChooseButtonStyle), typeof(Style), typeof(ChooseBox)); #endregion #region ChooseBoxType public ChooseType ChooseType { get => (ChooseType)GetValue(ChooseTypeProperty); set => SetValue(ChooseTypeProperty, value); } public static readonly DependencyProperty ChooseTypeProperty = DependencyProperty.Register( nameof(ChooseType), typeof(ChooseType), typeof(ChooseBox), new PropertyMetadata(ChooseType.SingleFile, OnChooseTypeChanged)); private static void OnChooseTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is ChooseBox chooseBox) { switch ((ChooseType)e.NewValue) { case ChooseType.SingleFile: chooseBox.PlaceholderText = "请选择文件"; break; case ChooseType.MultiFiles: chooseBox.PlaceholderText = "请选择多个文件"; break; case ChooseType.Folder: chooseBox.PlaceholderText = "请选择文件夹"; break; case ChooseType.SaveFile: chooseBox.PlaceholderText = "请选择保存文件路径"; break; } } } #endregion #region ChooseButtonWidth public double ChooseButtonWidth { get => (double)GetValue(ChooseButtonWidthProperty); set => SetValue(ChooseButtonWidthProperty, value); } public static readonly DependencyProperty ChooseButtonWidthProperty = DependencyProperty.Register( nameof(ChooseButtonWidth), typeof(double), typeof(ChooseBox), new PropertyMetadata(20d)); #endregion #region ChooseBoxFilter public string Filter { get => (string)GetValue(FilterProperty); set => SetValue(FilterProperty, value); } public static readonly DependencyProperty FilterProperty = DependencyProperty.Register( nameof(Filter), typeof(string), typeof(ChooseBox), new PropertyMetadata("文本文件|*.*")); #endregion #region DefaultFileName public string DefaultFileName { get => (string)GetValue(DefaultFileNameProperty); set => SetValue(DefaultFileNameProperty, value); } public static readonly DependencyProperty DefaultFileNameProperty = DependencyProperty.Register( nameof(DefaultFileName), typeof(string), typeof(ChooseBox), new PropertyMetadata("文件")); #endregion #region DefaultExt public string DefaultExt { get => (string)GetValue(DefaultExtProperty); set => SetValue(DefaultExtProperty, value); } public static readonly DependencyProperty DefaultExtProperty = DependencyProperty.Register( nameof(DefaultExt), typeof(string), typeof(ChooseBox), new PropertyMetadata(string.Empty)); #endregion }