2010-10-28 62 views
3

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

回答

-1

它看起来像你需要一对夫妇件...首先在这里对您添加

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

和Format事件的Reflector'd位再有就是这个:

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

+0

:S您是否偶然回答了另一个问题? – Juan 2010-10-28 18:37:28

+0

@jsoldi〜你不会使用RedGate反射器吗? – jcolebrand 2010-10-28 18:39:11

+0

根本不是...... – Juan 2010-10-28 18:40:34

相关问题