2024-09-22 11:05:41 +08:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Windows.Data;
|
|
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
namespace ShrlAlgo.Toolkit.Mvvm.Converters;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
|
|
|
|
|
public class ComparisonConverter : IValueConverter
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <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)
|
|
|
|
|
|
{
|
|
|
|
|
|
//value:ViewModel属性,枚举/数字/字符串
|
|
|
|
|
|
//parameter:ConverterParameter,枚举/数字/字符串
|
|
|
|
|
|
//return (string)parameter == (string)value;
|
|
|
|
|
|
//绑定的枚举属性与控件转换参数一致则选中
|
|
|
|
|
|
return value.Equals(parameter);
|
|
|
|
|
|
//return (string)parameter == (string)value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 控件选中,将Converter参数传给模型属性
|
|
|
|
|
|
/// </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)
|
|
|
|
|
|
{
|
|
|
|
|
|
//value:bool 比如RadioButton的check属性
|
|
|
|
|
|
//parameter:ConverterParameter枚举
|
|
|
|
|
|
//一样才传递给ViewModel
|
|
|
|
|
|
//选中则返回枚举类
|
|
|
|
|
|
return value is true ? parameter : Binding.DoNothing;
|
|
|
|
|
|
//return (bool)value ? parameter : Binding.DoNothing;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|