What does the formattingEnabled parameter do in the Binding constructor?

Here is the documentation. I have not found an explanation anywhere. There is a data binding review , but for WPF, and I use WinForms. I thought that it should call any method that I assign to the Formatclass event Binding, but it will call it even if I set formattingEnabledto false if I assign a method. So now I don’t know what he is doing, and I don’t understand where people should receive such information.

+3
source share
1 answer

It looks like you need a few pieces ... First, here is the Reflector'd bit in the Format event that you 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);
    }
}

and then this:

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

, , .

-1

Source: https://habr.com/ru/post/1771978/


All Articles