44 lines
1.6 KiB
C#
44 lines
1.6 KiB
C#
using System.Globalization;
|
||
using System.Windows.Data;
|
||
|
||
namespace NeumUI.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();
|
||
}
|
||
} |