功能更新
This commit is contained in:
41
Melskin/Converters/NullOrEmptyConverter.cs
Normal file
41
Melskin/Converters/NullOrEmptyConverter.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Globalization;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace Melskin.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// 支持判断是否为 null 或空字符串,以及是否为非 null 且非空字符串的转换器
|
||||
/// </summary>
|
||||
public class NullOrEmptyConverter : IValueConverter
|
||||
{
|
||||
private readonly bool isNotNullOrEmpty;
|
||||
|
||||
/// <summary>
|
||||
/// 判断对象是否为 null 或空字符串的单例实例
|
||||
/// </summary>
|
||||
public static readonly NullOrEmptyConverter Instance = new(false);
|
||||
|
||||
/// <summary>
|
||||
/// 判断对象是否为非 null 且非空字符串的单例实例
|
||||
/// </summary>
|
||||
public static readonly NullOrEmptyConverter NotInstance = new(true);
|
||||
|
||||
private NullOrEmptyConverter(bool isNotNullOrEmpty = false)
|
||||
{
|
||||
this.isNotNullOrEmpty = isNotNullOrEmpty;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
// 如果值为 null 或空字符串,则返回 true;否则返回 false
|
||||
var result = value == null || string.IsNullOrEmpty(value.ToString());
|
||||
return isNotNullOrEmpty ? !result : result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
return DependencyProperty.UnsetValue;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user