WPF:文本框和双精度绑定无法输入 . 在上面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30318135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:08:28  来源:igfitidea点击:

WPF: Textbox and Binding to Double not able to type . on it

wpfmvvm

提问by Sabyasachi Mishra

I have a text box like

我有一个像

<TextBox Text="{Binding TransactionDetails.TransactionAmount, Mode=TwoWay, 
UpdateSourceTrigger=PropertyChanged}" Grid.Column="3" Grid.ColumnSpan="2" Grid.Row="5" 
x:Name="TextBoxAmount"/>

And I have taken "TransactionAmount" as Double. Its working well on integer value but when I am typing some Floating point value like 100.456 I am not able to type '.'

我将“TransactionAmount”设为双倍。它在整数值上运行良好,但是当我输入一些浮点值(如 100.456)时,我无法输入 '.'

回答by Herm

You are updating your property every time the value changes. When you type in a ., it is written into your viewmodel and the view is updated.

每次值更改时,您都在更新您的属性。当您输入 a 时.,它会被写入您的视图模型并更新视图。

e.g. if you type in 100.it is rounded to 100, thus you won't see any dot ever.

例如,如果您输入100.它会四舍五入到100,因此您永远不会看到任何点。

You have some options to change this behavior:

您有一些选项可以更改此行为:

use a deferred binding:

使用延迟绑定:

<TextBox Text="{Binding Path=TransactionDetails.TransactionAmount, 
                        Mode=TwoWay, 
                        UpdateSourceTrigger=PropertyChanged, 
                        Delay=250}" 
         Grid.Column="3" 
         Grid.ColumnSpan="2" 
         Grid.Row="5" 
         x:Name="TextBoxAmount" />

only change the value if it is different from the saved one (I'd recommend this for every binding):

仅更改与保存的值不同的值(我建议对每个绑定都这样做):

private double _transactionAmount; 
public double TransactionAmount  
{
  get { return _transactionAmount; }    
  set
  { 
    if (_transactionAmount != value)
    {
      _transactionAmount = value; 
      Notify("TransactionAmount"); 
    }
  }

or use some kind of validation, e.g. ValidatesOnExceptions.

或使用某种验证,例如 ValidatesOnExceptions。

回答by Sabyasachi Mishra

The best solution I got by using StringFormatlike

我使用StringFormatlike得到的最佳解决方案

<TextBox Text="{Binding TransactionDetails.TransactionAmount, Mode=TwoWay, 
UpdateSourceTrigger=PropertyChanged,StringFormat=N2}" Grid.Column="3" 
Grid.ColumnSpan="2" Grid.Row="5" x:Name="TextBoxAmount" />

Also we can go for custom string format as per requirements

我们也可以根据要求使用自定义字符串格式

回答by Dinesh balan

Your Problem is with UpdateSourceTrigger. Instead of using there You can use something like this,

您的问题出在 UpdateSourceTrigger 上。而不是使用那里你可以使用这样的东西,

private double amount;
public double Amount
    {
        get
        {
            return amount;
        }
        set
        {
            amount= value;
            PropertyChanged();
            Calculation();
        }
    }

PropertyChanged() You will get this from INotifyPropertyChanged. For more Information click here https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

PropertyChanged() 您将从 INotifyPropertyChanged 获得此信息。有关更多信息,请单击此处 https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

回答by Hemanth Suresh

I am giving answer based on the answer of Herm above. The explanation is correct, but using Delayon control won't solve the issue fully. If the end user types 0.005, the delay required will be more, otherwise it will re-write value as 0.

我是根据上面 Herm 的回答给出答案的。解释是正确的,但使用Delayon control 并不能完全解决问题。如果最终用户键入 0.005,则所需的延迟会更多,否则将重新写入值为 0。

Instead, Use a string property for binding and try to parse it to double and based on parse output set the long value you need. Put all kind of validations you need before setting the value

相反,使用字符串属性进行绑定并尝试将其解析为双精度并根据解析输出设置您需要的长值。在设置值之前放置您需要的所有类型的验证

private double _amount;
private string _amountString;
public string Amount
{
    get { return _amountString;}
    set {
            double d=0;
            if(Double.TryParse(value, out d))
            {
                _amountString=value;
                _amount=d;
            }
        }
   }
}

回答by Aakanksha

In binding of the property use, UpdateSourceTrigger=LostFocus. It will update the property once textbox is out of focus.

在属性使用的绑定中,UpdateSourceTrigger=LostFocus. 一旦文本框失去焦点,它将更新该属性。