Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding to double field with validation
    primarykey
    data
    text
    <p>I'm trying to bind <code>TextBox</code> to <code>double</code> property of some object with <code>UpdateSourceTrigger=PropertyChanged</code>. The goal is to immediately during editing validate entered value to be in allowed range (and display an error if not). I want to implement validation on Model level, i.e. via <code>IDataErrorInfo</code>.</p> <p>All works great when I bind to int property, but if property is double then a frustrating editing behavior appears: after erasing last significant digit in fractional part of number - the decimal separator is automatically erased (with all possible fractional zeroes). For example, after erasing digit '3' from number '12.03' the text is changed to '12' instead of '12.0'.</p> <p>Please, help.</p> <p>Here is the sample code:</p> <p>MainWindow.xaml:</p> <pre><code>&lt;Window x:Class="BindWithValidation.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="80" Width="200" WindowStartupLocation="CenterOwner"&gt; &lt;StackPanel&gt; &lt;TextBox Width="100" Margin="10" Text="{Binding DoubleField, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"&gt; &lt;TextBox.Style&gt; &lt;Style TargetType="TextBox"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="Validation.HasError" Value="true"&gt; &lt;Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/TextBox.Style&gt; &lt;/TextBox&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>MainWindow.xaml.cs:</p> <pre><code>namespace BindWithValidation { public partial class MainWindow : Window { private UISimpleData _uiData = new UISimpleData(); public MainWindow() { InitializeComponent(); DataContext = _uiData; } } } </code></pre> <p>UISimpleData.cs:</p> <pre><code>namespace BindWithValidation { public class UISimpleData : INotifyPropertyChanged, IDataErrorInfo { private double _doubleField = 12.03; public double DoubleField { get { return _doubleField; } set { if (_doubleField == value) return; _doubleField = value; RaisePropertyChanged("DoubleField"); } } public string this[string propertyName] { get { string validationResult = null; switch (propertyName) { case "DoubleField": { if (DoubleField &lt; 2 || DoubleField &gt; 5) validationResult = "DoubleField is out of range"; break; } default: throw new ApplicationException("Unknown Property being validated on UIData"); } return validationResult; } } public string Error { get { return "not implemented"; } } public event PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChanged(string property) { if ( PropertyChanged != null ) PropertyChanged(this, new PropertyChangedEventArgs(property)); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload