命令更新
This commit is contained in:
@@ -355,8 +355,7 @@ public class MultiComboBox : Control
|
||||
{
|
||||
base.OnApplyTemplate();
|
||||
|
||||
if (listBox != null)
|
||||
listBox.SelectionChanged -= ListBox_SelectionChanged;
|
||||
listBox?.SelectionChanged -= ListBox_SelectionChanged;
|
||||
|
||||
listBox = GetTemplateChild("PART_ListBox") as ListBox;
|
||||
filterTextBox = GetTemplateChild("PART_FilterTextBox") as TextBox;
|
||||
@@ -564,8 +563,7 @@ public class MultiComboBox : Control
|
||||
private static void OnSelectionModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var c = (MultiComboBox)d;
|
||||
if (c.listBox != null)
|
||||
c.listBox.SelectionMode = c.SelectionMode;
|
||||
c.listBox?.SelectionMode = c.SelectionMode;
|
||||
|
||||
c.isUpdatingSelection = true;
|
||||
try
|
||||
@@ -620,7 +618,7 @@ public class MultiComboBox : Control
|
||||
// 1. 移除 ListBox 中有但源中没有的 (倒序遍历以安全删除)
|
||||
for (int i = controlList.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (!sourceSet.Contains(controlList[i]))
|
||||
if (!sourceSet.Contains(controlList[i]!))
|
||||
{
|
||||
controlList.RemoveAt(i);
|
||||
}
|
||||
@@ -763,13 +761,13 @@ public class MultiComboBox : Control
|
||||
}
|
||||
|
||||
// 处理第一个元素
|
||||
if (IsMatch(firstItem, filter!, propDesc)) filtered.Add(firstItem);
|
||||
if (IsMatch(firstItem, filter!, propDesc)) filtered.Add(firstItem!);
|
||||
}
|
||||
|
||||
// 处理剩余元素
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
if (IsMatch(enumerator.Current, filter, propDesc))
|
||||
if (IsMatch(enumerator.Current, filter!, propDesc))
|
||||
filtered.Add(enumerator.Current);
|
||||
}
|
||||
|
||||
@@ -791,6 +789,6 @@ public class MultiComboBox : Control
|
||||
value = item.ToString();
|
||||
}
|
||||
|
||||
return value != null && value.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0;
|
||||
return value != null && value.Contains(filter);
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ namespace Melskin.Controls
|
||||
/// </summary>
|
||||
public class SearchableComboBox : ComboBox
|
||||
{
|
||||
private TextBox editableTextBox;
|
||||
private TextBox? editableTextBox;
|
||||
private bool isInternalOperation;
|
||||
|
||||
static SearchableComboBox()
|
||||
@@ -31,11 +31,13 @@ namespace Melskin.Controls
|
||||
public override void OnApplyTemplate()
|
||||
{
|
||||
base.OnApplyTemplate();
|
||||
if (GetTemplateChild("PART_EditableTextBox") is TextBox _editableTextBox)
|
||||
editableTextBox = GetTemplateChild("PART_EditableTextBox") as TextBox;
|
||||
|
||||
if (editableTextBox != null)
|
||||
{
|
||||
_editableTextBox.TextChanged += OnEditableTextBoxTextChanged;
|
||||
editableTextBox.TextChanged += OnEditableTextBoxTextChanged;
|
||||
// 点击时全选
|
||||
_editableTextBox.GotFocus += (s, e) => _editableTextBox.SelectAll();
|
||||
editableTextBox.GotFocus += (s, e) => editableTextBox.SelectAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +56,7 @@ namespace Melskin.Controls
|
||||
private void OnEditableTextBoxTextChanged(object sender, TextChangedEventArgs e)
|
||||
{
|
||||
if (isInternalOperation) return;
|
||||
|
||||
if (editableTextBox == null) return;
|
||||
string searchText = editableTextBox.Text;
|
||||
string selectedText = GetItemDisplayText(SelectedItem);
|
||||
|
||||
@@ -75,7 +77,7 @@ namespace Melskin.Controls
|
||||
string itemText = GetItemDisplayText(item);
|
||||
|
||||
// 只要包含关键字就显示(忽略大小写)
|
||||
return itemText.IndexOf(searchText, StringComparison.OrdinalIgnoreCase) >= 0;
|
||||
return itemText.Contains(searchText);
|
||||
};
|
||||
|
||||
// 自动打开下拉框
|
||||
|
||||
@@ -7,10 +7,10 @@ namespace Melskin.Utilities;
|
||||
internal class RelayCommand : ICommand
|
||||
{
|
||||
private readonly Action<object?> execute;
|
||||
private readonly Predicate<object?>? _canExecute;
|
||||
private readonly Predicate<object?>? canExecute;
|
||||
/// <summary>
|
||||
/// 当命令的可执行状态更改时发生的事件。
|
||||
/// 此事件通知UI,当命令的CanExecute方法返回值发生变化时,需要重新查询命令的状态。
|
||||
/// 广播此事件通知UI,当命令的CanExecute方法返回值发生变化时,需要重新查询命令的状态。
|
||||
/// </summary>
|
||||
public event EventHandler? CanExecuteChanged
|
||||
{
|
||||
@@ -18,16 +18,23 @@ internal class RelayCommand : ICommand
|
||||
remove => CommandManager.RequerySuggested -= value;
|
||||
}
|
||||
/// <summary>
|
||||
/// 一个简单的ICommand实现,用于传递动作。此命令始终可以执行。
|
||||
/// 初始化 RelayCommand 的新实例。
|
||||
/// </summary>
|
||||
public RelayCommand(Action<object?> execute) => this.execute = execute;
|
||||
/// <param name="execute">命令执行的逻辑。</param>
|
||||
/// <param name="canExecute">判断命令是否可以执行的逻辑(可选)。如果为 null,则命令始终可执行。</param>
|
||||
/// <exception cref="ArgumentNullException">execute 为 null 时抛出。</exception>
|
||||
public RelayCommand(Action<object?> execute, Predicate<object?>? canExecute = null)
|
||||
{
|
||||
this.execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
||||
this.canExecute = canExecute;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断命令是否可以执行。
|
||||
/// </summary>
|
||||
/// <param name="parameter">传递给命令的参数。</param>
|
||||
/// <returns>返回一个布尔值,指示命令是否能够执行。对于此实现,总是返回true。</returns>
|
||||
public bool CanExecute(object? parameter) => _canExecute == null || _canExecute(parameter);
|
||||
public bool CanExecute(object? parameter) => canExecute == null || canExecute(parameter);
|
||||
|
||||
/// <summary>
|
||||
/// 执行命令。
|
||||
@@ -36,79 +43,8 @@ internal class RelayCommand : ICommand
|
||||
public void Execute(object? parameter) => execute(parameter);
|
||||
|
||||
/// <summary>
|
||||
/// 通知命令管理器重新查询此命令的 CanExecute 状态。
|
||||
/// 通知命令管理器重新查询此命令的 CanExecute 状态,手动更新,应对一些特殊场景,如倒计时功能结束,手动调用刷新按钮状态。
|
||||
/// </summary>
|
||||
public void RaiseCanExecuteChanged() => CommandManager.InvalidateRequerySuggested();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 一个简单的ICommand实现,用于传递动作。此命令始终可以执行。
|
||||
/// </summary>
|
||||
internal class RelayCommand<T> : ICommand
|
||||
{
|
||||
private readonly Action<T?> execute;
|
||||
private readonly Predicate<T?>? canExecute;
|
||||
|
||||
/// <summary>
|
||||
/// 一个简单的ICommand实现,用于传递动作。此命令始终可以执行。
|
||||
/// </summary>
|
||||
public RelayCommand(Action<T?> execute, Predicate<T?>? canExecute = null)
|
||||
{
|
||||
this.execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
||||
this.canExecute = canExecute;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当命令的可执行状态更改时触发的事件。
|
||||
/// 此事件允许UI元素订阅并响应命令可执行性变化,从而能够适时更新其状态(如启用或禁用按钮)。
|
||||
/// 通过将此事件与CommandManager.RequerySuggested关联,可以确保每当应用程序中的命令状态可能改变时,UI都会自动检查命令是否仍然可执行。
|
||||
/// </summary>
|
||||
public event EventHandler? CanExecuteChanged
|
||||
{
|
||||
add => CommandManager.RequerySuggested += value;
|
||||
remove => CommandManager.RequerySuggested -= value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断命令是否可以执行。
|
||||
/// </summary>
|
||||
/// <param name="parameter">传递给命令的参数。</param>
|
||||
/// <returns>返回一个布尔值,指示命令是否能够执行。对于此实现,总是返回true。</returns>
|
||||
public bool CanExecute(object? parameter)
|
||||
{
|
||||
if (canExecute == null) return true;
|
||||
|
||||
// 使用安全的类型获取机制,防止 InvalidCastException
|
||||
T? validParameter = GetSafeParameter(parameter);
|
||||
return canExecute(validParameter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行命令。
|
||||
/// </summary>
|
||||
/// <param name="parameter">传递给命令的参数。</param>
|
||||
public void Execute(object? parameter)
|
||||
{
|
||||
T? validParameter = GetSafeParameter(parameter);
|
||||
execute(validParameter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通知命令管理器重新查询此命令的CanExecute状态。
|
||||
/// 此方法用于在命令执行条件可能已更改时更新UI。
|
||||
/// </summary>
|
||||
public void RaiseCanExecuteChanged() => CommandManager.InvalidateRequerySuggested();
|
||||
/// <summary>
|
||||
/// 安全地将 object 参数转换为泛型 T。如果类型不匹配,则返回 T 的默认值。
|
||||
/// </summary>
|
||||
private static T? GetSafeParameter(object? parameter)
|
||||
{
|
||||
if (parameter is T tParam)
|
||||
{
|
||||
return tParam;
|
||||
}
|
||||
|
||||
// 应对 WPF 绑定时可能会传入 null 的情况,特别是在 T 为值类型时
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
82
Melskin/Utilities/RelayCommand{T}.cs
Normal file
82
Melskin/Utilities/RelayCommand{T}.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace Melskin.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// 一个简单的ICommand实现,用于传递动作。此命令始终可以执行。
|
||||
/// </summary>
|
||||
internal class RelayCommand<T> : ICommand
|
||||
{
|
||||
private readonly Action<T?> execute;
|
||||
private readonly Predicate<T?>? canExecute;
|
||||
|
||||
/// <summary>
|
||||
/// 一个简单的ICommand实现,用于传递动作。此命令始终可以执行。
|
||||
/// </summary>
|
||||
public RelayCommand(Action<T?> execute, Predicate<T?>? canExecute = null)
|
||||
{
|
||||
this.execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
||||
this.canExecute = canExecute;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当命令的可执行状态更改时触发的事件。
|
||||
/// 广播此事件允许UI元素订阅并响应命令可执行性变化,从而能够适时更新其状态(如启用或禁用按钮)。
|
||||
/// 通过将此事件与CommandManager.RequerySuggested关联,可以确保每当应用程序中的命令状态可能改变时,UI都会自动检查命令是否仍然可执行。
|
||||
/// </summary>
|
||||
public event EventHandler? CanExecuteChanged
|
||||
{
|
||||
add => CommandManager.RequerySuggested += value;
|
||||
remove => CommandManager.RequerySuggested -= value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断命令是否可以执行。
|
||||
/// </summary>
|
||||
/// <param name="parameter">传递给命令的参数。</param>
|
||||
/// <returns>返回一个布尔值,指示命令是否能够执行。对于此实现,总是返回true。</returns>
|
||||
public bool CanExecute(object? parameter)
|
||||
{
|
||||
if (canExecute == null) return true;
|
||||
|
||||
// 使用安全的类型获取机制,防止 InvalidCastException
|
||||
T? validParameter = GetSafeParameter(parameter);
|
||||
return canExecute(validParameter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行命令。
|
||||
/// </summary>
|
||||
/// <param name="parameter">传递给命令的参数。</param>
|
||||
public void Execute(object? parameter)
|
||||
{
|
||||
T? validParameter = GetSafeParameter(parameter);
|
||||
execute(validParameter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通知命令管理器重新查询此命令的CanExecute状态。
|
||||
/// 此方法用于在命令执行条件可能已更改时更新UI,手动更新,应对一些特殊场景,如倒计时功能结束,手动调用刷新按钮状态
|
||||
/// </summary>
|
||||
public void RaiseCanExecuteChanged() => CommandManager.InvalidateRequerySuggested();
|
||||
/// <summary>
|
||||
/// 安全地将 object 参数转换为泛型 T。如果类型不匹配,则返回 T 的默认值。
|
||||
/// </summary>
|
||||
private static T? GetSafeParameter(object? parameter)
|
||||
{
|
||||
if (parameter is T tParam)
|
||||
{
|
||||
return tParam;
|
||||
}
|
||||
|
||||
// 应对 WPF 绑定时可能会传入 null 的情况,特别是在 T 为值类型时
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user