Files
ShrlAlgoToolkit/Wpf.Ui.Extend/Controls/ChooseBox/ChooseBox.xaml.cs
2024-09-22 11:05:41 +08:00

157 lines
4.0 KiB
C#

using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
namespace Wpf.Ui.Extend.Controls;
public class ChooseBox : TextBox
{
static ChooseBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ChooseBox), new FrameworkPropertyMetadata(typeof(ChooseBox)));
}
private Button PART_ChooseButton;
#region ChooseButtonStyle
/// <summary>
/// 获取或者设置选择按钮的样式
/// </summary>
public Style ChooseButtonStyle
{
get { return (Style)GetValue(ChooseButtonStyleProperty); }
set { SetValue(ChooseButtonStyleProperty, value); }
}
public static readonly DependencyProperty ChooseButtonStyleProperty = DependencyProperty.Register(
nameof(ChooseButtonStyle),
typeof(Style),
typeof(ChooseBox)
);
#endregion
#region ChooseBoxType
public ChooseBoxType ChooseBoxType
{
get { return (ChooseBoxType)GetValue(ChooseBoxTypeProperty); }
set { SetValue(ChooseBoxTypeProperty, value); }
}
public static readonly DependencyProperty ChooseBoxTypeProperty = DependencyProperty.Register(
nameof(ChooseBoxType),
typeof(ChooseBoxType),
typeof(ChooseBox),
new PropertyMetadata(ChooseBoxType.SingleFile)
);
#endregion
#region ChooseButtonWidth
public double ChooseButtonWidth
{
get { return (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 { return (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 { return (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 { return (string)GetValue(DefaultExtProperty); }
set { SetValue(DefaultExtProperty, value); }
}
public static readonly DependencyProperty DefaultExtProperty = DependencyProperty.Register(
nameof(DefaultExt),
typeof(string),
typeof(ChooseBox),
new PropertyMetadata("")
);
#endregion
#region Override
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.PART_ChooseButton = this.GetTemplateChild("PART_ChooseButton") as Button;
if (this.PART_ChooseButton != null)
{
this.PART_ChooseButton.Click += PART_ChooseButton_Click;
}
}
#endregion
#region Event Implement Function
private void PART_ChooseButton_Click(object sender, RoutedEventArgs e)
{
switch (this.ChooseBoxType)
{
case ChooseBoxType.SingleFile:
OpenFileDialog openFileDialog =
new()
{
Multiselect = false,
//"文本文件|*.*|C#文件|*.cs|所有文件|*.*"
Filter = this.Filter
};
if (openFileDialog.ShowDialog() == true)
this.Text = openFileDialog.FileName;
break;
case ChooseBoxType.MultiFile:
break;
case ChooseBoxType.Folder:
var folderDialog = new VistaFolderBrowserDialog();
if (folderDialog.ShowDialog() == true)
this.Text = folderDialog.SelectedPath;
break;
case ChooseBoxType.SaveFile:
var fileDialog = new SaveFileDialog
{
Filter = this.Filter, //设置文件类型
FileName = this.DefaultFileName, //设置默认文件名
DefaultExt = this.DefaultExt, //设置默认格式(可以不设)
AddExtension = true //设置自动在文件名中添加扩展名
};
if (fileDialog.ShowDialog() == true)
this.Text = fileDialog.FileName;
break;
}
}
#endregion
}