Files
ShrlAlgoToolkit/NeuWPF/NeoUI/Converters/Internal/ValueToRangeWidthConverter.cs

44 lines
1.6 KiB
C#
Raw Normal View History

2025-07-31 20:12:24 +08:00
using System.Globalization;
2025-07-11 09:20:23 +08:00
using System.Windows.Data;
2025-08-20 12:10:13 +08:00
namespace NeumUI.Converters.Internal;
2025-08-12 23:08:54 +08:00
/// <summary>
/// 该转换器实现了IMultiValueConverter接口用于将值转换为范围宽度。它主要用于进度条或滑块控件中根据当前值、最小值和最大值来计算并返回一个宽度值。
/// 通过传入的参数(以字符串形式),可以额外指定边距,该边距会从总宽度中减去。最终返回的宽度基于当前值在最小值与最大值之间的比例,并且考虑了调整后的实际宽度。
/// </summary>
internal class ValueToRangeWidthConverter : IMultiValueConverter
2025-07-11 09:20:23 +08:00
{
2025-08-20 12:10:13 +08:00
public static readonly ValueToRangeWidthConverter Instance = new();
2025-08-12 23:08:54 +08:00
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
2025-07-11 09:20:23 +08:00
{
2025-08-12 23:08:54 +08:00
var paramStr = parameter as string;
_ = double.TryParse(paramStr, out var margin);
2025-07-11 09:20:23 +08:00
2025-08-12 23:08:54 +08:00
var minimum = (double)values[0];
var maximum = (double)values[1];
var currentValue = (double)values[2];
var width = (double)values[3];
var actualWidth = width - 2 * margin;
2025-07-11 09:20:23 +08:00
2025-08-12 23:08:54 +08:00
if (actualWidth < 0)
{
actualWidth = 0;
2025-07-11 09:20:23 +08:00
}
2025-08-12 23:08:54 +08:00
if (currentValue < minimum)
2025-07-11 09:20:23 +08:00
{
2025-08-12 23:08:54 +08:00
return 0;
2025-07-11 09:20:23 +08:00
}
2025-08-12 23:08:54 +08:00
if (currentValue > maximum)
{
return actualWidth;
}
return ((currentValue - minimum) / (maximum - minimum)) * actualWidth;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
2025-07-11 09:20:23 +08:00
}
2025-08-12 23:08:54 +08:00
}