Files
ShrlAlgoToolkit/Melskin/Converters/Internal/ValueToRangeWidthConverter.cs
2026-02-17 22:17:13 +08:00

44 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Globalization;
using System.Windows.Data;
namespace Melskin.Converters.Internal;
/// <summary>
/// 该转换器实现了IMultiValueConverter接口用于将值转换为范围宽度。它主要用于进度条或滑块控件中根据当前值、最小值和最大值来计算并返回一个宽度值。
/// 通过传入的参数(以字符串形式),可以额外指定边距,该边距会从总宽度中减去。最终返回的宽度基于当前值在最小值与最大值之间的比例,并且考虑了调整后的实际宽度。
/// </summary>
internal class ValueToRangeWidthConverter : IMultiValueConverter
{
public static readonly ValueToRangeWidthConverter Instance = new();
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var paramStr = parameter as string;
_ = double.TryParse(paramStr, out var margin);
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;
if (actualWidth < 0)
{
actualWidth = 0;
}
if (currentValue < minimum)
{
return 0;
}
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();
}
}