mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +00:00
62 lines
1.9 KiB
C#
62 lines
1.9 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Windows.Data;
|
|
using System.Windows.Markup;
|
|
|
|
namespace ServerManagerTool.Common.Converters
|
|
{
|
|
public class IntRangeValueConverter : MarkupExtension, IValueConverter
|
|
{
|
|
public const string DEFAULT_CULTURE_CODE = "en-US";
|
|
protected int MinValue { get; set; }
|
|
protected int MaxValue { get; set; }
|
|
|
|
public IntRangeValueConverter()
|
|
{
|
|
MinValue = int.MinValue;
|
|
MaxValue = int.MaxValue;
|
|
}
|
|
|
|
public IntRangeValueConverter(int minValue)
|
|
{
|
|
MinValue = minValue;
|
|
MaxValue = int.MaxValue;
|
|
}
|
|
|
|
public IntRangeValueConverter(int minValue, int maxValue)
|
|
{
|
|
MinValue = minValue;
|
|
MaxValue = maxValue;
|
|
}
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
var scaledValue = System.Convert.ToInt32(value);
|
|
|
|
var sliderValue = scaledValue;
|
|
sliderValue = Math.Max(MinValue, sliderValue);
|
|
sliderValue = Math.Min(MaxValue, sliderValue);
|
|
return sliderValue;
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (value is null || value.ToString() == string.Empty)
|
|
return default;
|
|
|
|
if (!int.TryParse(value.ToString(), NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.GetCultureInfo(DEFAULT_CULTURE_CODE), out int sliderValue))
|
|
return default;
|
|
|
|
sliderValue = Math.Max(MinValue, sliderValue);
|
|
sliderValue = Math.Min(MaxValue, sliderValue);
|
|
|
|
var scaledValue = sliderValue;
|
|
return scaledValue;
|
|
}
|
|
|
|
public override object ProvideValue(IServiceProvider serviceProvider)
|
|
{
|
|
return this;
|
|
}
|
|
}
|
|
}
|