OneWayToSource Binding seems broken in .NET 4.0
I have this simple piece of Xaml
I had a variation of this issue for a two way binding. I realise that this is not exactly the same as the problem being discussed but this answer came up top while searching and it lead to my solution. Hope someone finds it helpful.
My solution blocks the re-read of the backing property but will update the UI when it is changed by some other source. I based my solution on the blocking converter in Rick Sladkey's answer.
It just adds a check to the convert to see if the lastValue field would convert to the same backing store value. If not, the backing store value must have changed from another source and the UI should be updated.
public class MyConverter : IValueConverter
{
public object lastValue;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (LastValue != null && MyConvertBack(LastValue).Equals(value))
return lastValue;
else
return MyConvert(value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
lastValue = value;
return MyConvertBack(value);
}
private object MyConvertBack(Object value)
{
//Conversion Code Here
}
private object MyConvert(Object value)
{
//Conversion Code Here
}
}
In my particular usecase for this I had a length and dimension suffix stored in a textbox (10m, 100mm etc). The converter parsed this to a double value or added the suffix (depending on direction of conversion). Without the converter it would add a suffix on each update of the textbox. Trying to type '10' would result in '1m0' as the converter would run after the first key stroke.