Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] MinMaxValueToBoolConverter Error #49

Open
momj-00 opened this issue Aug 30, 2024 · 3 comments
Open

[Bug] MinMaxValueToBoolConverter Error #49

momj-00 opened this issue Aug 30, 2024 · 3 comments
Assignees
Labels

Comments

@momj-00
Copy link

momj-00 commented Aug 30, 2024

屏幕截图 2024-08-30 114817

@momj-00 momj-00 added the bug label Aug 30, 2024
@thomasgalliker
Copy link
Owner

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

@thomasgalliker thomasgalliker self-assigned this Aug 30, 2024
@momj-00
Copy link
Author

momj-00 commented Aug 30, 2024

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
Copy link

zmrbak commented Aug 30, 2024

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.");
}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants