This commit is contained in:
ShrlAlgo
2025-08-20 12:10:35 +08:00
parent fcd306b0f7
commit 955a01f564
962 changed files with 7893 additions and 127784 deletions

View File

@@ -1,8 +1,8 @@
using System.Windows.Controls.Primitives;
using NeumUI.Extensions;
using NeoUI.Extensions;
namespace NeumUI.Assists;
namespace NeoUI.Assists;
/// <summary>
/// InputAssist 类提供了一系列附加属性,用于增强 WPF 应用程序中输入控件的功能。这些功能包括设置占位符文本、占位符颜色、前缀和后缀内容等,支持 TextBoxBase、PasswordBox 和 ComboBox 控件。
@@ -18,7 +18,7 @@ public static class InputAssist
DependencyProperty.RegisterAttached("Placeholder", typeof(string), typeof(InputAssist), new PropertyMetadata(string.Empty));
/// <summary>
/// Gets the placeholder for the input control.
/// 获取输入控件的占位符文本。
/// </summary>
[AttachedPropertyBrowsableForType(typeof(TextBoxBase))]
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
@@ -50,7 +50,7 @@ public static class InputAssist
FrameworkPropertyMetadataOptions.Inherits));
/// <summary>
/// Gets the placeholder foreground brush for the input control.
/// 获取输入控件的占位符画刷。
/// </summary>
[AttachedPropertyBrowsableForType(typeof(TextBoxBase))]
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
@@ -107,8 +107,16 @@ public static class InputAssist
typeof(InputAssist),
new PropertyMetadata(false));
/// <summary>
/// 用于获取或设置是否显示密码框内有内容的指示器。
/// </summary>
public static readonly DependencyProperty HasPasswordProperty = HasTextPropertyKey.DependencyProperty;
/// <summary>
/// 获取指定控件是否启用了密码模式。
/// </summary>
/// <param name="obj">要检查的DependencyObject对象。</param>
/// <returns>如果控件启用了密码模式则返回true否则返回false。</returns>
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
public static bool GetHasPassword(DependencyObject obj)
{
@@ -129,8 +137,10 @@ public static class InputAssist
DependencyProperty.RegisterAttached("Suffix", typeof(object), typeof(InputAssist), new PropertyMetadata(null));
/// <summary>
/// Gets the suffix object of the input control.
/// 获取附加到指定控件的后缀内容。
/// </summary>
/// <param name="obj">要获取后缀内容的DependencyObject对象。</param>
/// <returns>返回控件的后缀内容如果未设置则为null。</returns>
[AttachedPropertyBrowsableForType(typeof(TextBox))]
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
public static object GetSuffix(DependencyObject obj)
@@ -189,7 +199,7 @@ public static class InputAssist
}
/// <summary>
/// Gets the password currently held by the PasswordBox.
/// 获取输入控件的密码文本。
/// </summary>
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
public static string GetPassword(DependencyObject obj)
@@ -198,7 +208,7 @@ public static class InputAssist
}
/// <summary>
/// Sets the password currently held by the PasswordBox.
/// 设置输入控件的密码文本。
/// </summary>
public static void SetPassword(DependencyObject obj, string value)
{
@@ -237,8 +247,10 @@ public static class InputAssist
DependencyProperty.RegisterAttached("ClearEnabled", typeof(bool), typeof(InputAssist), new PropertyMetadata(false, OnClearEnabledChanged));
/// <summary>
/// Gets the clear enable state of the control.
/// 获取指定控件是否启用了清除功能。
/// </summary>
/// <param name="obj">要获取属性的依赖对象。</param>
/// <returns>如果启用了清除功能,则返回 true否则返回 false。</returns>
[AttachedPropertyBrowsableForType(typeof(TextBox))]
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
[AttachedPropertyBrowsableForType(typeof(ComboBox))]
@@ -249,7 +261,7 @@ public static class InputAssist
/// <summary>
/// Sets the clear enable state of the control.
/// 设置输入控件是否可以被清空。
/// </summary>
public static void SetClearEnabled(DependencyObject obj, bool value)
{
@@ -269,14 +281,27 @@ public static class InputAssist
}
}
/// <summary>
/// 用于设置或获取控件是否显示清除按钮。
/// </summary>
public static readonly DependencyProperty ClearableProperty = DependencyProperty.RegisterAttached(
"Clearable", typeof(bool), typeof(InputAssist), new PropertyMetadata(default(bool)));
/// <summary>
/// 设置输入控件是否可清除。
/// </summary>
/// <param name="d">要设置属性的依赖对象。</param>
/// <param name="value">布尔值,表示输入控件是否可以被用户清除其内容。</param>
public static void SetClearable(DependencyObject d, bool value)
{
d.SetValue(ClearableProperty, value);
}
/// <summary>
/// 获取指定依赖对象的可清除属性值。
/// </summary>
/// <param name="d">要获取属性值的依赖对象。</param>
/// <returns>返回一个布尔值,表示该控件是否可以被清除内容。</returns>
public static bool GetClearable(DependencyObject d)
{
return (bool)d.GetValue(ClearableProperty);
@@ -287,62 +312,60 @@ public static class InputAssist
private static void OnClear(object sender, RoutedEventArgs e)
{
if (sender is DependencyObject d)
if (sender is not DependencyObject d) return;
//var ance = d.GetAncestors();
var parent = d.GetAncestors().FirstOrDefault(a => a is TextBox or PasswordBox or ComboBox);
if (parent != null && !GetClearable(parent)) return;
if (parent is TextBox textBox)
{
//var ance = d.GetAncestors();
var parent = d.GetAncestors().FirstOrDefault(a => a is TextBox || a is PasswordBox || a is ComboBox);
if (!GetClearable(parent)) return;
if (parent is TextBox textBox)
{
textBox.Clear();
textBox.GetBindingExpression(TextBox.TextProperty)?.UpdateSource();
}
else if (parent is PasswordBox passwordBox)
{
passwordBox.Clear();
passwordBox.GetBindingExpression(PasswordProperty)?.UpdateSource();
}
else if (parent is ComboBox)
{
if (((ComboBox)parent).IsEditable)
{
((ComboBox)parent).Text = string.Empty;
((ComboBox)parent).GetBindingExpression(ComboBox.TextProperty)?.UpdateSource();
}
((ComboBox)parent).SelectedItem = null;
((ComboBox)parent).GetBindingExpression(Selector.SelectedItemProperty)?.UpdateSource();
}
// if (d is TextBox textBox)
// {
// textBox.Clear();
// textBox.GetBindingExpression(TextBox.TextProperty)?.UpdateSource();
// }
// else if (d is PasswordBox passwordBox)
// {
// passwordBox.Clear();
// passwordBox.GetBindingExpression(PasswordProperty)?.UpdateSource();
// }
// else if (d is ComboBox comboBox)
// {
// if (comboBox.IsEditable)
// {
// comboBox.Text = string.Empty;
// comboBox.GetBindingExpression(ComboBox.TextProperty)?.UpdateSource();
// }
//
// comboBox.SelectedItem = null;
// comboBox.GetBindingExpression(Selector.SelectedItemProperty)?.UpdateSource();
// }
// else if(d is AutoComplete auto)
// {
// // 如果是 AutoComplete 控件,清空其文本
// // 并更新绑定源
// auto.Text = string.Empty;
// auto.GetBindingExpression(TextBox.TextProperty)?.UpdateSource();
// }
textBox.Clear();
textBox.GetBindingExpression(TextBox.TextProperty)?.UpdateSource();
}
else if (parent is PasswordBox passwordBox)
{
passwordBox.Clear();
passwordBox.GetBindingExpression(PasswordProperty)?.UpdateSource();
}
else if (parent is ComboBox)
{
if (((ComboBox)parent).IsEditable)
{
((ComboBox)parent).Text = string.Empty;
((ComboBox)parent).GetBindingExpression(ComboBox.TextProperty)?.UpdateSource();
}
((ComboBox)parent).SelectedItem = null;
((ComboBox)parent).GetBindingExpression(Selector.SelectedItemProperty)?.UpdateSource();
}
// if (d is TextBox textBox)
// {
// textBox.Clear();
// textBox.GetBindingExpression(TextBox.TextProperty)?.UpdateSource();
// }
// else if (d is PasswordBox passwordBox)
// {
// passwordBox.Clear();
// passwordBox.GetBindingExpression(PasswordProperty)?.UpdateSource();
// }
// else if (d is ComboBox comboBox)
// {
// if (comboBox.IsEditable)
// {
// comboBox.Text = string.Empty;
// comboBox.GetBindingExpression(ComboBox.TextProperty)?.UpdateSource();
// }
//
// comboBox.SelectedItem = null;
// comboBox.GetBindingExpression(Selector.SelectedItemProperty)?.UpdateSource();
// }
// else if(d is AutoComplete auto)
// {
// // 如果是 AutoComplete 控件,清空其文本
// // 并更新绑定源
// auto.Text = string.Empty;
// auto.GetBindingExpression(TextBox.TextProperty)?.UpdateSource();
// }
}
#endregion