它看起來像你需要一對夫婦件...首先在這裏對您添加
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事件處理程序的內容格式化對象。
:S您是否偶然回答了另一個問題? – Juan 2010-10-28 18:37:28
@jsoldi〜你不會使用RedGate反射器嗎? – jcolebrand 2010-10-28 18:39:11
根本不是...... – Juan 2010-10-28 18:40:34