Files
ShrlAlgoToolkit/NeuWPF/NeoUI/Controls/ChooseBox.xaml.cs

223 lines
7.2 KiB
C#
Raw Normal View History

2025-08-12 23:08:54 +08:00

using Microsoft.Win32;
2025-08-20 12:10:13 +08:00
namespace NeumUI.Controls;
2025-08-12 23:08:54 +08:00
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(nameof(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)
{
var 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
/// <summary>
/// 获取或者设置选择按钮的样式
/// </summary>
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
}
2025-08-20 12:10:13 +08:00
/// <summary>
/// 定义选择框的选择类型。
/// </summary>
2025-08-12 23:08:54 +08:00
public enum ChooseType
{
2025-08-20 12:10:13 +08:00
/// <summary>
/// 选择单个文件。当设置为该选项时,将打开一个文件对话框允许用户选择单个文件。
/// </summary>
2025-08-12 23:08:54 +08:00
SingleFile,
2025-08-20 12:10:13 +08:00
/// <summary>
/// 表示选择类型为多文件。当设置此选项时,用户可以选择多个文件。
/// </summary>
2025-08-12 23:08:54 +08:00
MultiFiles,
2025-08-20 12:10:13 +08:00
/// <summary>
/// 表示选择类型为文件夹。当设置此选项时,用户将能够通过对话框选择一个文件夹路径。
/// </summary>
2025-08-12 23:08:54 +08:00
Folder,
2025-08-20 12:10:13 +08:00
/// <summary>
/// 选择类型为保存文件。当此枚举成员被选中时,将显示一个“另存为”对话框,允许用户选择文件的保存位置和名称。
/// </summary>
2025-08-12 23:08:54 +08:00
SaveFile,
}