What is the use of ConvertBack method in IValueConverter interface?
Solution 1
IMO, the ConvertBack
method is used to convert your visual representation of the data to the specific DataType.
For example: you use a Converter to convert a boolean true
to the string "TrueBoolean"
. This text will be displayed in your TextBox. When you change the value of the TextBox, the ConvertBack
method will be called as soon as the binding fires again (default OnFocusLost). Now your ConvertBack
method will try to convert the new value to the datatype you want it to be. So you will have to implement logic to convert "FalseBoolean"
to false
.
public class Converter : IValueConverter
{
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool) value ? "TrueBoolean" : "FalseBoolean";
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var s = (string) value;
if (s.Equals("TrueBoolean",StringComparison.CurrentCultureIgnoreCase))
return true;
if (s.Equals("FalseBoolean", StringComparison.CurrentCultureIgnoreCase))
return false;
throw new Exception(string.Format("Cannot convert, unknown value {0}", value));
}
}
This technique is used a lot in DataGrids if I'm not mistaken.
Hope this is a bit clear...
UPDATE
About you question in the comment:
To overwrite the default OnFocusLost
binding behavior you have to change your binding like this:
<TextBox Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}"/>
<!--syntax might differ, can't access VS at the moment.-->
Solution 2
ConvertBack
is when you have mode=TwoWay
on your binding.
It converts the user input back into the datatype that you're binding to and gets invoked when the TextBox
(say) loses focus.
For example (if this wasn't built in), if you have a number that represents a price, the Convert
method would take the value and format it into a string with the correct currency symbol, decimal separator etc.
If the user types in a new value - including possible currency symbol, thousands separator etc. you'd use the ConvertBack
method to parse the string and extract the numeric value.
Another example could be credit card number input. The user could enter the number as a single string of digits or groups of digits separated by spaces or dashes. The ConvertBack
method would take all these possible inputs and convert them to the single format you require.
Solution 3
check the UpdateSourceTrigger Property on your Binding. http://msdn.microsoft.com/library/system.windows.data.binding.updatesourcetrigger.aspx
The textbox will by default update the source if it looses focus, using the UpdateSourceTrigger you can set this behaviour to immediate update of the source if the textbox content changes.
HTH Dominik
Related videos on Youtube
Aryan SuryaWansi
Updated on July 09, 2022Comments
-
Aryan SuryaWansi almost 2 years
What is the use of
ConvertBack
method in theIValueConverter
interface.When will it be called?
Or what is the order of invocation of the
Convert
andConvertBack
methods?I have asked the question here because: I have bound one property of codebehind to TEXTBOX’s TEXT Property and am using
convertor
for that property. The firstConvert
Method invokes and when I changeTEXT
inTEXTBOX
nothing happens... but as soon as I close the form theConvertBack
method invokes. -
Aryan SuryaWansi about 13 years@Roel, ya but in my case ConvertBack is fire when i closing the window, is n't it a strange?
-
RoelF about 13 yearsno, I think this is normal behavior: Before the window is closed, the OnFocusLost event of the TextBox will be fired, or at least the binding will update.
-
Aryan SuryaWansi about 13 years@Roel, what should i have to do if i need to have call ConvertBack method as soon as any change make in TEXT of TEXTBOX? i could n't find update source trigger property as @Dominik says...
-
Max Mazur over 8 years@Roel, How about INotifyPropertyChanged interface? Do you need to implement that on your property in order for the "UpdateSourceTrigger=propertychanged" to work? and when is a "Mode=TwoWay" binding relevant? You seem to know your way around this area.
-
RoelF over 8 years@MaxMazur yes you need to implement
INotifyPropertyChanged
. AndTwoWay
binding is the default for most controls (you can checkBindsTwoWayByDefault
on the control to confirm). It is relevant for TextBoxes and Checkboxes for example, where it is important that the value can be upated either via code or via user input.