参数formattingEnabled在Binding的构造函数中做了什么?

时间:2010-10-28 17:59:22

标签: c# .net winforms

Here是文档。我在任何地方都没有找到任何解释。有一个数据绑定overview但是用于WPF,我正在使用WinForms。我认为它的作用是调用我分配给Format类的事件Binding的任何方法,但即使我将formattingEnabled设置为false,它也会调用它,只要我指定一个方法。所以现在我不知道它做了什么,我不明白人们应该从哪里得到这种信息。

2 个答案:

答案 0 :(得分:0)

十年后,这个问题出现了,并没有找到特别有用的答案。现在的来源是available online。只是看评论:

d

Whidbey,RTM和Everett在Visual Basic中似乎是不同的代号,因此在我看来,如果要使用FormatObject的“最新”版本,应该使用private object FormatObject(object value) { ... if (formattingEnabled) { // ------------------------------- // Behavior for Whidbey and beyond // ------------------------------- ... } else { // ---------------------------- // Behavior for RTM and Everett [DO NOT MODIFY!] // ---------------------------- ... // Approved breaking-change behavior between RTM and Everett: Fire the Format event even if the control property is of type // Object (RTM used to skip the event for properties of this type). NOTE: This change contains a bug (fixed in the new // Whidbey logic above); Everett always returns the *original* object in this case, ignoring any attempt by the event handler // to replace this with a different object. ... } }

答案 1 :(得分:-1)

看起来你需要几件......首先,这是您添加的Format事件上的Reflector'd位

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事件处理程序的内容来格式化对象。