thomasgalliker / ValueConverters.NET

A collection of commonly used IValueConverters for .NET applications
MIT License
140 stars 25 forks source link

[Bug] MinMaxValueToBoolConverter Error #49

Open momj-00 opened 3 weeks ago

momj-00 commented 3 weeks ago

屏幕截图 2024-08-30 114817

thomasgalliker commented 3 weeks ago

Can you please use the bug report template (as far as possible) to describe what's going wrong?

momj-00 commented 3 weeks ago

Description

Intput Value > MaxValue ,But Return True; IntputValue = "2999", MinValue="10' , MaxValue="30"

Steps to Reproduce

Use MinMaxValueToBoolConverter Or IsInRangeConverter

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:vc="clr-namespace:ValueConverters;assembly=ValueConverters"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="100" Width="200">
    <Window.Resources>
        <vc:MinMaxValueToBoolConverter x:Key="MinMaxValueToBoolConverter" MinValue="10" MaxValue="30" />
    </Window.Resources>
    <StackPanel>        
        <TextBox x:Name="txt1" Text="2999"/>
        <Label Content="{Binding ElementName=txt1, Path=Text, Converter={StaticResource MinMaxValueToBoolConverter}}"/>
    </StackPanel>
</Window>

Expected Behavior

Return False

Actual Behavior

Return True

image

zmrbak commented 3 weeks ago

This code come from AI.

` protected override object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null || this.MinValue == null || this.MaxValue == null) { return DependencyProperty.UnsetValue; }

// 尝试将 MinValue 和 MaxValue 转换为与 value 相同的类型
Type valueType = value.GetType();
object minValue = Convert.ChangeType(this.MinValue, valueType, culture);
object maxValue = Convert.ChangeType(this.MaxValue, valueType, culture);

// 检查是否可以比较
if (!(value is IComparable))
{
    throw new InvalidOperationException("Value must implement IComparable interface.");
}

// 进行比较
if (valueType == typeof(DateTime))
{
    // 日期类型比较
    return ((DateTime)value) >= (DateTime)minValue && ((DateTime)value) <= (DateTime)maxValue;
}
else if (valueType == typeof(TimeSpan))
{
    // 时间跨度类型比较
    return ((TimeSpan)value) >= (TimeSpan)minValue && ((TimeSpan)value) <= (TimeSpan)maxValue;
}
else if (valueType == typeof(bool))
{
    // 布尔类型比较
    return (bool)value >= (bool)minValue && (bool)value <= (bool)maxValue;
}
else if (valueType.IsEnum)
{
    // 枚举类型比较
    return ((int)value) >= (int)minValue && ((int)value) <= (int)maxValue;
}
else if (valueType == typeof(double) || valueType == typeof(float))
{
    // 浮点类型比较,考虑精度问题
    double valueAsDouble = Convert.ToDouble(value);
    double minAsDouble = Convert.ToDouble(minValue);
    double maxAsDouble = Convert.ToDouble(maxValue);
    return valueAsDouble >= minAsDouble && valueAsDouble <= maxAsDouble;
}
else if (valueType == typeof(string))
{
    // 字符串类型比较
    return string.Compare((string)value, (string)minValue, culture) >= 0 && string.Compare((string)value, (string)maxValue, culture) <= 0;
}
else if (value is IComparable comparableValue)
{
    // 其他实现了 IComparable 的类型
    return comparableValue.CompareTo(minValue) >= 0 && comparableValue.CompareTo(maxValue) >= 0;
}
else
{
    throw new InvalidOperationException("Unsupported type for comparison.");
}

} `