Files
ShrlAlgoToolkit/Wpf.Ui.Extend/Converters/StringToColorConverter.cs
2024-09-22 11:05:41 +08:00

53 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;
using Wpf.Ui.Controls;
namespace Wpf.Ui.Extend.Converters;
public class StringToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string str = value.ToString();
Regex regex = new Regex("^#(?:[0-9a-fA-F]{3}){1,2}$");
if (regex.IsMatch(str))
{
var color = ColorConverter.ConvertFromString(str);
return color;
}
return Binding.DoNothing;
}
}
/// <summary>
/// 判断值是否为空
/// </summary>
public class StringIsEmptyConverter : IValueConverter
{
#region IValueConverter
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return string.IsNullOrEmpty(System.Convert.ToString(value));
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
#endregion
}