IValueConverter接口中的ConvertBack方法有什么用?
IValueConverter
接口中的 ConvertBack
方法有什么用。
什么时候调用?
或者Convert
和ConvertBack
方法的调用顺序是什么?
我在这里问这个问题是因为:我已将代码隐藏的一个属性绑定到TEXTBOX的TEXT属性,并且正在为该属性使用convertor
。第一个 Convert
方法调用,当我更改 TEXTBOX
中的 TEXT
时,没有任何反应......但是一旦我关闭表单,ConvertBack
方法调用。
What is the use of ConvertBack
method in the IValueConverter
interface.
When will it be called?
Or what is the order of invocation of the Convert
and ConvertBack
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 first Convert
Method invokes and when I change TEXT
in TEXTBOX
nothing happens... but as soon as I close the form the ConvertBack
method invokes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
IMO,
ConvertBack
方法用于将数据的视觉表示形式转换为特定的 DataType。例如:您使用转换器将布尔值
true
转换为字符串"TrueBoolean"
。该文本将显示在您的文本框中。当您更改 TextBox 的值时,一旦绑定再次触发(默认 OnFocusLost),就会调用ConvertBack
方法。现在,您的ConvertBack
方法将尝试将新值转换为您想要的数据类型。因此,您必须实现将"FalseBoolean"
转换为false
的逻辑。如果我没记错的话,这种技术在 DataGrid 中被大量使用。
希望这有点清楚......
更新
关于评论中您的问题:
要覆盖默认的
OnFocusLost
绑定行为,您必须像这样更改绑定: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, theConvertBack
method will be called as soon as the binding fires again (default OnFocusLost). Now yourConvertBack
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"
tofalse
.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:ConvertBack
是指当您的绑定上有mode=TwoWay
时。它将用户输入转换回您要绑定的数据类型,并在
TextBox
(例如)失去焦点时调用。例如(如果不是内置的),如果您有一个代表价格的数字,
Convert
方法将获取该值并将其格式化为具有正确货币符号、小数分隔符的字符串 如果用户输入新值 - 包括可能的货币符号、千位分隔符等,您将使用
ConvertBack
方法来解析字符串并提取数值。另一个例子是信用卡号输入。用户可以将数字输入为由空格或破折号分隔的单个数字串或数字组。
ConvertBack
方法将获取所有这些可能的输入并将它们转换为您需要的单一格式。ConvertBack
is when you havemode=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.检查绑定上的 UpdateSourceTrigger 属性。
http://msdn.microsoft.com/library/system.windows .data.binding.updatesourcetrigger.aspx
默认情况下,文本框在失去焦点时将更新源,使用 UpdateSourceTrigger 您可以将此行为设置为在文本框内容更改时立即更新源。
华泰
多米尼克
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