更新
This commit is contained in:
27
ShrlAlgoToolkit.Mvvm/Converters/BooleanConverter.cs
Normal file
27
ShrlAlgoToolkit.Mvvm/Converters/BooleanConverter.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Converters;
|
||||
|
||||
public class BooleanConverter<T> : IValueConverter
|
||||
{
|
||||
public BooleanConverter(T trueValue, T falseValue)
|
||||
{
|
||||
TrueValue = trueValue;
|
||||
FalseValue = falseValue;
|
||||
}
|
||||
|
||||
public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
|
||||
value is bool boolValue &&
|
||||
boolValue
|
||||
? TrueValue
|
||||
: FalseValue;
|
||||
|
||||
public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value is T tValue &&
|
||||
EqualityComparer<T>.Default.Equals(tValue, TrueValue);
|
||||
|
||||
public T FalseValue { get; set; }
|
||||
|
||||
public T TrueValue { get; set; }
|
||||
}
|
||||
13
ShrlAlgoToolkit.Mvvm/Converters/BooleanToVisConverter.cs
Normal file
13
ShrlAlgoToolkit.Mvvm/Converters/BooleanToVisConverter.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Converters;
|
||||
|
||||
public class BooleanToVisConverter : BooleanConverter<Visibility>
|
||||
{
|
||||
public BooleanToVisConverter() : base(Visibility.Visible, Visibility.Collapsed)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ using System.Windows.Media;
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Converters;
|
||||
|
||||
public class ColorToBrushConverter: IValueConverter
|
||||
public class ColorToBrushConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
|
||||
28
ShrlAlgoToolkit.Mvvm/Converters/EnumDescriptionExtension.cs
Normal file
28
ShrlAlgoToolkit.Mvvm/Converters/EnumDescriptionExtension.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Windows.Markup;
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Converters
|
||||
{
|
||||
public class EnumDescriptionExtension : MarkupExtension
|
||||
{
|
||||
private readonly object _enumValue;
|
||||
|
||||
public EnumDescriptionExtension(object enumValue)
|
||||
{
|
||||
_enumValue = enumValue;
|
||||
}
|
||||
|
||||
public override object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
if (_enumValue == null) return string.Empty;
|
||||
|
||||
var field = _enumValue.GetType().GetField(_enumValue.ToString());
|
||||
var attribute = field?.GetCustomAttributes(typeof(DescriptionAttribute), false)
|
||||
.FirstOrDefault() as DescriptionAttribute;
|
||||
|
||||
return attribute?.Description ?? _enumValue.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ namespace ShrlAlgoToolkit.Mvvm.Converters;
|
||||
/// <summary>
|
||||
/// 枚举类上加特性[TypeConverter(typeof(EnumDescriptionTypeConverter))]
|
||||
/// </summary>
|
||||
//TODO 待测试
|
||||
public class EnumDescriptionTypeConverter : EnumConverter
|
||||
{
|
||||
public EnumDescriptionTypeConverter(Type type) : base(type)
|
||||
|
||||
@@ -11,83 +11,83 @@ namespace ShrlAlgoToolkit.Mvvm.Converters;
|
||||
/// </summary>
|
||||
public class EnumItemsSourceAndConverter : Collection<string>, IValueConverter
|
||||
{
|
||||
private IDictionary<object, object> displayNameToValueMap;
|
||||
private Type type;
|
||||
private IDictionary<object, object> displayNameToValueMap;
|
||||
private Type type;
|
||||
|
||||
private IDictionary<object, object> valueToDisplayNameMap;
|
||||
private IDictionary<object, object> valueToDisplayNameMap;
|
||||
|
||||
public Type Type
|
||||
{
|
||||
get => type;
|
||||
set
|
||||
{
|
||||
if (!value.IsEnum)
|
||||
{
|
||||
throw new ArgumentException("不是枚举类型", $"{value}");
|
||||
}
|
||||
public Type Type
|
||||
{
|
||||
get => type;
|
||||
set
|
||||
{
|
||||
if (!value.IsEnum)
|
||||
{
|
||||
throw new ArgumentException("不是枚举类型", $"{value}");
|
||||
}
|
||||
|
||||
type = value;
|
||||
Initialize();
|
||||
}
|
||||
}
|
||||
type = value;
|
||||
Initialize();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 类属性到控件属性,源到目标
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="targetType"></param>
|
||||
/// <param name="parameter"></param>
|
||||
/// <param name="culture"></param>
|
||||
/// <returns></returns>
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
//if (value != null)
|
||||
//{
|
||||
// int n = (int)value;
|
||||
// if (n == 0 || n == -1)
|
||||
// {
|
||||
// return null;
|
||||
// }
|
||||
//}
|
||||
return value == null
|
||||
? null
|
||||
: valueToDisplayNameMap.TryGetValue(value, out var value1)
|
||||
? value1
|
||||
: null;
|
||||
}
|
||||
/// <summary>
|
||||
/// 类属性到控件属性,源到目标
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="targetType"></param>
|
||||
/// <param name="parameter"></param>
|
||||
/// <param name="culture"></param>
|
||||
/// <returns></returns>
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
//if (value != null)
|
||||
//{
|
||||
// int n = (int)value;
|
||||
// if (n == 0 || n == -1)
|
||||
// {
|
||||
// return null;
|
||||
// }
|
||||
//}
|
||||
return value == null
|
||||
? null
|
||||
: valueToDisplayNameMap.TryGetValue(value, out var value1)
|
||||
? value1
|
||||
: null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 目标到源
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="targetType"></param>
|
||||
/// <param name="parameter"></param>
|
||||
/// <param name="culture"></param>
|
||||
/// <returns></returns>
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return value == null
|
||||
? null
|
||||
: displayNameToValueMap.TryGetValue(value, out var value1)
|
||||
? value1
|
||||
: null;
|
||||
}
|
||||
/// <summary>
|
||||
/// 目标到源
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="targetType"></param>
|
||||
/// <param name="parameter"></param>
|
||||
/// <param name="culture"></param>
|
||||
/// <returns></returns>
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return value == null
|
||||
? null
|
||||
: displayNameToValueMap.TryGetValue(value, out var value1)
|
||||
? value1
|
||||
: null;
|
||||
}
|
||||
|
||||
private static object GetDescription(FieldInfo fieldInfo)
|
||||
{
|
||||
var descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute));
|
||||
return descriptionAttribute != null ? descriptionAttribute.Description : fieldInfo.Name;
|
||||
}
|
||||
private static object GetDescription(FieldInfo fieldInfo)
|
||||
{
|
||||
var descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute));
|
||||
return descriptionAttribute != null ? descriptionAttribute.Description : fieldInfo.Name;
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
valueToDisplayNameMap = type.GetFields(BindingFlags.Static | BindingFlags.Public).ToDictionary(fi => fi.GetValue(null), GetDescription);
|
||||
displayNameToValueMap = valueToDisplayNameMap.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
|
||||
Clear();
|
||||
//当前converter的集合添加元素
|
||||
foreach (string name in displayNameToValueMap.Keys)
|
||||
{
|
||||
Add(name);
|
||||
}
|
||||
}
|
||||
private void Initialize()
|
||||
{
|
||||
valueToDisplayNameMap = type.GetFields(BindingFlags.Static | BindingFlags.Public).ToDictionary(fi => fi.GetValue(null), GetDescription);
|
||||
displayNameToValueMap = valueToDisplayNameMap.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
|
||||
Clear();
|
||||
//当前converter的集合添加元素
|
||||
foreach (string name in displayNameToValueMap.Keys)
|
||||
{
|
||||
Add(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,13 @@ namespace ShrlAlgoToolkit.Mvvm.Converters;
|
||||
|
||||
public class InvertBooleanConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return value is bool b ? !b : value;
|
||||
}
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return value is bool b ? !b : value;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return value is bool b ? !b : value;
|
||||
}
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return value is bool b ? !b : value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using System.Windows;
|
||||
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Converters;
|
||||
|
||||
public class InverterBooleanToVisConverter : BooleanConverter<Visibility>
|
||||
{
|
||||
public InverterBooleanToVisConverter() : base(Visibility.Collapsed, Visibility.Visible)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
82
ShrlAlgoToolkit.Mvvm/Converters/MarkupConverters.cs
Normal file
82
ShrlAlgoToolkit.Mvvm/Converters/MarkupConverters.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Markup;
|
||||
|
||||
namespace ShrlAlgo.Toolkit.Wpf.Converters
|
||||
{
|
||||
[ValueConversion(typeof(bool), typeof(Visibility))]
|
||||
public class BoolVisibilityConverter : MarkupExtension, IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return (value is bool b && b) ? Visibility.Visible : Visibility.Collapsed;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return value is Visibility v && v == Visibility.Visible;
|
||||
}
|
||||
|
||||
public override object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
[ValueConversion(typeof(bool), typeof(Visibility))]
|
||||
public class InverseBoolVisibilityConverter : MarkupExtension, IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return (bool)value! == false ? Visibility.Visible : Visibility.Collapsed;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return (Visibility)value! != Visibility.Visible;
|
||||
}
|
||||
|
||||
public override object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
||||
[ValueConversion(typeof(bool), typeof(bool))]
|
||||
public class BooleanConverter : MarkupExtension, IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return (bool)value!;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return (bool)value!;
|
||||
}
|
||||
|
||||
public override object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
||||
[ValueConversion(typeof(bool), typeof(bool))]
|
||||
public class InverseBooleanConverter : MarkupExtension, IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return !(bool)value!;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return !(bool)value!;
|
||||
}
|
||||
|
||||
public override object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts null or not null to a <see cref="Visibility" />.
|
||||
/// </summary>
|
||||
public class NotNullToVisibilityConverter : IValueConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// The visibility value if the argument is null.
|
||||
/// </summary>
|
||||
public Visibility NullValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="NotNullToVisibilityConverter" />.
|
||||
/// </summary>
|
||||
public NotNullToVisibilityConverter()
|
||||
{
|
||||
NullValue = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return value == null ? NullValue : Visibility.Visible;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return Binding.DoNothing;
|
||||
}
|
||||
}
|
||||
}
|
||||
20
ShrlAlgoToolkit.Mvvm/Converters/NullToBooleanConverter.cs
Normal file
20
ShrlAlgoToolkit.Mvvm/Converters/NullToBooleanConverter.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Converters
|
||||
{
|
||||
public class NullToBooleanConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return value != null;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user