C# TextBox绑定到double并输入小于-1的负数时出现问题
当我将文本框绑定到Property并输入一个小于-1的负数时,我遇到了一个问题-例如-0.45: 文本框:C# TextBox绑定到double并输入小于-1的负数时出现问题,c#,wpf,C#,Wpf,当我将文本框绑定到Property并输入一个小于-1的负数时,我遇到了一个问题-例如-0.45: 文本框: <TextBox Text="{Binding Txt, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 似乎当我尝试输入-0.54时,它会立即变为0,为什么?当您输入十进制值时,它会再次变为0,因此最好的方法是使用lostfocus触发器: <TextBox Text="{Binding Txt, Mode=
<TextBox Text="{Binding Txt, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
似乎当我尝试输入-0.54时,它会立即变为0,为什么?当您输入十进制值时,它会再次变为0,因此最好的方法是使用lostfocus触发器:
<TextBox Text="{Binding Txt, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" Grid.Row="0"/>
当您输入十进制值时,它再次变为0,因此最好的方法是使用lostfocus触发器:
<TextBox Text="{Binding Txt, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" Grid.Row="0"/>
这是执行此任务的转换器(因此保持视图模型不变-您可以将其用于十进制和双精度)。我们只需要先保持小数点和-ve的位置:
public class DecimalConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value !=null)
{
return value.ToString();
}
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string data = value as string;
if (data == null)
{
return value;
}
if (data.Equals(string.Empty))
{
return 0;
}
if (!string.IsNullOrEmpty(data))
{
decimal result;
//Hold the value if ending with .
if (data.EndsWith(".") || data.Equals("-0"))
{
return Binding.DoNothing;
}
if (decimal.TryParse(data, out result))
{
return result;
}
}
return Binding.DoNothing;
}
}
因此,我们在绑定时保留值或不执行任何操作这是执行此任务的转换器(因此,保持视图模型不变-您可以将其用于十进制和双精度)。我们只需要先保持小数点和-ve的位置:
public class DecimalConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value !=null)
{
return value.ToString();
}
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string data = value as string;
if (data == null)
{
return value;
}
if (data.Equals(string.Empty))
{
return 0;
}
if (!string.IsNullOrEmpty(data))
{
decimal result;
//Hold the value if ending with .
if (data.EndsWith(".") || data.Equals("-0"))
{
return Binding.DoNothing;
}
if (decimal.TryParse(data, out result))
{
return result;
}
}
return Binding.DoNothing;
}
}
因此,我们在绑定时保留这些值或不执行任何操作您是否需要在每次文本更改时更新视图模型,就像您现在使用
UpdateSourceTrigger=PropertyChanged
所做的那样,或者您是否可以删除该值并在失去焦点时默认更新视图模型?每次你按下一个键,WPF需要将string
转换为double
,当你将值发布到查看模型时,当你输入-0时,它将解析为0。你可以使用文本和验证数据错误,但这是一项小工作。或者,您可以使用UpdateSourceTrigger=Explicit,如果以-开头,则不进行更新,直到您得到除0.BTW以外的数字-045不小于-1;)。您是否需要在每次文本更改时都更新视图模型,就像您现在使用的UpdateSourceTrigger=PropertyChanged
一样,或者您是否可以删除该视图模型并在默认情况下在失去焦点时更新视图模型?每次你按下一个键,WPF需要将string
转换为double
,当你将值发布到查看模型时,当你输入-0时,它将解析为0。你可以使用文本和验证数据错误,但这是一项小工作。或者,您可以使用UpdateSourceTrigger=Explicit,如果以-开头,则不进行更新,直到您得到除0.BTW以外的数字-045不小于-1;)。你会在双精度上使用Equals吗?是的,刚刚试过双精度d1=10;双d2=10;布尔等质量=d1。等于(d2);唯一的另一种方法是使用if(Math.Abs(double1-double2)这里有一个好的开始:我知道我可以使用lostfocus,但我不想使用它,不是吗?也许Converter在这里很好?你会在双精度上使用Equals吗?是的,只是尝试了它double d1=10;double d2=10;bool isequal=d1.Equals(d2);唯一的其他方法是使用if(Math.Abs(double1-double2)这是一个好的开始:我知道我可以使用lostfocus,但我不想使用它,不是吗?也许Converetter在这里很好?这正是我同步滑动控件和文本框所需要的。很好!这正是我同步滑动控件和文本框所需要的。很好!