什么是的IValueConverter接口使用ConvertBack方法? [英] What is the use of ConvertBack method in IValueConverter interface?

查看:179
本文介绍了什么是的IValueConverter接口使用ConvertBack方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是使用 ConvertBack的方法的IValueConverter 接口。

什么时候会打电话?

或者是什么为了调用 的转换 ConvertBack 方法。

我问的问题在这里,因为:我已绑定的一个属性codebehind 以TEXTBOX的Text属性,并使用转换该属性,

I have ask question here because: I have bound one property of codebehind to TEXTBOX’s TEXT Property and using convertor for that property,

那么首先转换方法调用,当我修改文本 TEXTBOX 没有发生,但一旦我关闭窗体 ConvertBack 方法调用。

then first Convert Method invoke and when I change TEXT in TEXTBOX nothing happen but as soon as I close the form ConvertBack method invoke.

这是什么,没有任何规则定义当火 ConvertBack 的方法?

what is this, there is not any rules that define when to fire ConvertBack method?

注意:您可能会发现,这是其他的问题,关于这个网站可能重复的......但不是我发现使用全在建议名单时,我写我的问题有任何疑问

NOTE: you may be find that this is possible duplicate of other question on this site...but not any question i have found use full in suggestion list when i writing my question.

推荐答案

海事组织, ConvertBack 方法被用于将你的视觉重新$ P $的数据psentation到具体的数据类型。

IMO, the ConvertBack method is used to convert your visual representation of the data to the specific DataType.

例如:你用转换器转换一个布尔值字符串TrueBoolean。该文本将显示在您的文本框。当您更改文本框的值时, ConvertBack 方法将被尽快再结合火灾(默认OnFocusLost)调用。现在你的 ConvertBack 方法将尝试新的值转换成你希望它是数据类型。所以,你必须实现逻辑转换FalseBoolean

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));
    }
}

该技术使用了大量的DataGrid中,如果我没有记错。

This technique is used a lot in DataGrids if I'm not mistaken.

希望这是一个有点清楚......

Hope this is a bit clear...

更新
关于你的问题在​​注释:
要覆盖默认 OnFocusLost 绑定行为,你必须改变你的绑定是这样的:

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.-->

这篇关于什么是的IValueConverter接口使用ConvertBack方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆