Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I realize I'm a little late to the party but I found a (I think) rather clean solution to this problem.</p> <p>A clever converter that remembers the last string converted to double and returns that if it exists should do everything you want.</p> <p>Note that when the user changes the contents of the textbox, ConvertBack will store the string the user input, parse the string for a double, and pass that value to the view model. Immediately after, Convert is called to display the newly changed value. At this point, the stored string is not null and will be returned.</p> <p>If the application instead of the user causes the double to change only Convert is called. This means that the cached string will be null and a standard ToString() will be called on the double.</p> <p>In this way, the user avoids strange surprises when modifying the contents of the textbox but the application can still trigger a change.</p> <pre><code>public class DoubleToPersistantStringConverter : IValueConverter { private string lastConvertBackString; public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (!(value is double)) return null; var stringValue = lastConvertBackString ?? value.ToString(); lastConvertBackString = null; return stringValue; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (!(value is string)) return null; double result; if (double.TryParse((string)value, out result)) { lastConvertBackString = (string)value; return result; } return null; } } </code></pre>
    singulars
    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.
    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