IValueConverter接口中的ConvertBack方法有什么用?

发布于 2024-11-16 13:36:47 字数 443 浏览 8 评论 0原文

IValueConverter 接口中的 ConvertBack 方法有什么用。

什么时候调用?

或者ConvertConvertBack方法的调用顺序是什么?

我在这里问这个问题是因为:我已将代码隐藏的一个属性绑定到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薆情海 2024-11-23 13:36:47

IMO,ConvertBack 方法用于将数据的视觉表示形式转换为特定的 DataType。

例如:您使用转换器将布尔值 true 转换为字符串 "TrueBoolean"。该文本将显示在您的文本框中。当您更改 TextBox 的值时,一旦绑定再次触发(默认 OnFocusLost),就会调用 ConvertBack 方法。现在,您的 ConvertBack 方法将尝试将新值转换为您想要的数据类型。因此,您必须实现将 "FalseBoolean" 转换为 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));
    }
}

如果我没记错的话,这种技术在 DataGrid 中被大量使用。

希望这有点清楚......

更新
关于评论中您的问题:
要覆盖默认的 OnFocusLost 绑定行为,您必须像这样更改绑定:

<TextBox Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}"/>
<!--syntax might differ, can't access VS at the moment.-->

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.-->
帅气尐潴 2024-11-23 13:36:47

ConvertBack 是指当您的绑定上有 mode=TwoWay 时。

它将用户输入转换回您要绑定的数据类型,并在 TextBox (例如)失去焦点时调用。

例如(如果不是内置的),如果您有一个代表价格的数字,Convert 方法将获取该值并将其格式化为具有正确货币符号、小数分隔符的字符串 如果用户输入新值 - 包括可能的货币符号、千位分隔符等

,您将使用 ConvertBack 方法来解析字符串并提取数值。

另一个例子是信用卡号输入。用户可以将数字输入为由空格或破折号分隔的单个数字串或数字​​组。 ConvertBack 方法将获取所有这些可能的输入并将它们转换为您需要的单一格式。

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.

指尖凝香 2024-11-23 13:36:47

检查绑定上的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文