0
votes

In our project we have a WPF textbox that is bound to a double. There is a converter that allows in the convertback to for example use both "." and "," to as decimal points and in the convert method it formats the double to the n2 numeric format. Here you can see a simplefied version of our converter:

public class DoubleConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null || value.GetType() != typeof(double))
            return null;
        else
            return ((double)value).ToString("n2");
    }


    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
           return null
        else
             return Double.Parse(((string)value).Replace('.',','));
    }
}

The textbox looks like this:

        <TextBox  Text="{Binding Path=Factor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Width="500" Height="50" />

And the double property raises a propertychanged event:

public double Factor
    {
        get { return _factor; }
        set
        {
            _factor = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Factor"));
        }
    }

This used to work fine in previous versions of WPF because the convert was'nt called when you were entering text. This behavior apparently has changed and now the convert method is called on every text entry, causing the double to be formatted when you type something.Even if you don't use formatting you have the problem of not being able to enter a decimal point.

This can be solve by not using UpdateSourceTrigger=Propertychanged, but we need this for validation. We implement validation by using the IDataErrorInterface. I know there is a ValidateWithoutUpdate method, but this doesn't work for validation using the IDataErrorInterface.

So what I basicly need is ConvertBack (and thus the validation) to happen OnPropertyChanged and the Convert to only happen OnLostFocus.
Is this possible? Or is there another solution for our problem?

1

1 Answers

0
votes

Yes, the behavior change from .Net 3.5 to .Net 4.0, where it now sends source updates back to targets even if the update originated from the target. This SO post explains it a bit and provides a solution:

Why does binding setup behave differently in .NET 4 vs .NET 3.5

These are the options I've found so far for dealing with this:

  • Have the converter maintain the state of the input on ConvertBack and restoring it in Convert (suggested in link above)
  • Change backing data to strings, handle conversion in model
  • Create a custom control that handles this kind of numeric input similar to how a DateTimePicker works (it maintains a string and double representation of the data; displays the string, but is bound by the double)