参数formattingEnabled在绑定的构造函数中有什么作用? [英] What does the parameter formattingEnabled do in Binding's constructor?

查看:100
本文介绍了参数formattingEnabled在绑定的构造函数中有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处是文档.我在任何地方都找不到任何解释.有一个数据绑定概述,但它用于WPF,我使用WinForms.我认为它的作用是调用分配给 Binding 类的事件 Format 的任何方法,但是即使我设置了 formattingEnabled 就会为false.因此,现在我不知道它的作用,也不知道人们应该从哪里获得这种信息.

Here's the documentation. I haven't found any explanation anywhere. There is a data binding overview but is for WPF, and I'm using WinForms. I thought what it does is to call whatever method I assign to the event Format of the Binding class, but it will call it even if I set formattingEnabled to false as long as I assign a method. So now I don't know what it does, and I don't understand where is people supposed to get this kind of information.

推荐答案

您似乎需要几块东西...首先,这是您添加的Format事件上的Reflector's bit

It looks like you need a couple pieces... First here's the Reflector'd bit on the Format event that you've added

protected virtual void OnFormat(ConvertEventArgs cevent)
{
    if (this.onFormat != null)
    {
        this.onFormat(this, cevent);
    }
    if (((!this.formattingEnabled && !(cevent.Value is DBNull)) && ((cevent.DesiredType != null) && !cevent.DesiredType.IsInstanceOfType(cevent.Value))) && (cevent.Value is IConvertible))
    {
        cevent.Value = Convert.ChangeType(cevent.Value, cevent.DesiredType, CultureInfo.CurrentCulture);
    }
}

然后是这个:

private object FormatObject(object value)
{
    if (this.ControlAtDesignTime())
    {
        return value;
    }
    Type propertyType = this.propInfo.PropertyType;
    if (this.formattingEnabled)
    {
        ConvertEventArgs args = new ConvertEventArgs(value, propertyType);
        this.OnFormat(args);
        if (args.Value != value)
        {
            return args.Value;
        }
        TypeConverter sourceConverter = null;
        if (this.bindToObject.FieldInfo != null)
        {
            sourceConverter = this.bindToObject.FieldInfo.Converter;
        }
        return Formatter.FormatObject(value, propertyType, sourceConverter, this.propInfoConverter, this.formatString, this.formatInfo, this.nullValue, this.dsNullValue);
    }
    ConvertEventArgs cevent = new ConvertEventArgs(value, propertyType);
    this.OnFormat(cevent);
    object obj2 = cevent.Value;
    if (propertyType == typeof(object))
    {
        return value;
    }
    if ((obj2 != null) && (obj2.GetType().IsSubclassOf(propertyType) || (obj2.GetType() == propertyType)))
    {
        return obj2;
    }
    TypeConverter converter2 = TypeDescriptor.GetConverter((value != null) ? value.GetType() : typeof(object));
    if ((converter2 != null) && converter2.CanConvertTo(propertyType))
    {
        return converter2.ConvertTo(value, propertyType);
    }
    if (value is IConvertible)
    {
        obj2 = Convert.ChangeType(value, propertyType, CultureInfo.CurrentCulture);
        if ((obj2 != null) && (obj2.GetType().IsSubclassOf(propertyType) || (obj2.GetType() == propertyType)))
        {
            return obj2;
        }
    }
    throw new FormatException(SR.GetString("ListBindingFormatFailed"));
}

因此,仍然要根据绑定到Format事件处理程序的内容来格式化对象.

So it's still going to format the object according to what you've bound to the Format event handler.

这篇关于参数formattingEnabled在绑定的构造函数中有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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