我们有一个自定义控件,具有类型为System.Nullable(也称为System.DateTime?)的“Value”属性。我们有一个具有相同类型的“Received”属性的对象。当我们试图将控件绑定到该对象时,会抛出以下无效强制转换异常:
从'System.DateTime'到'System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'的无效转换。
在应用程序中,这是异常抛出的位置:
正如您所看到的,对象的属性(this._object.DateTimeReceived)是System.DateTime?类型,控件的属性(this.dateReceived.Value)也是System.DateTime?类型。
为什么会导致InvalidCastException?我们该如何更正以使其正确绑定?
更新2009-10-29 14:26 CDT:
以下是堆栈跟踪:
在 System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) 处 在 System.DateTime.System.IConvertible.ToType(Type type, IFormatProvider provider) 处 在 System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) 处 在 System.Windows.Forms.Binding.FormatObject(Object value) 处 在 System.Windows.Forms.Binding.PushData(Boolean force) 处 在 System.Windows.Forms.Binding.UpdateIsBinding() 处 在 System.Windows.Forms.Binding.CheckBinding() 处 在 System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase) 处 在 System.Windows.Forms.ListManagerBindingsCollection.AddCore(Binding dataBinding) 处 在 System.Windows.Forms.BindingsCollection.Add(Binding binding) 处 在 System.Windows.Forms.BindingContext.UpdateBinding(BindingContext newBindingContext, Binding binding) 处 在 System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent value) 处 在 System.Windows.Forms.ControlBindingsCollection.AddCore(Binding dataBinding) 处 在 System.Windows.Forms.BindingsCollection.Add(Binding binding) 处 在 System.Windows.Forms.ControlBindingsCollection.Add(String propertyName, Object dataSource, String dataMember, Boolean formattingEnabled, DataSourceUpdateMode updateMode, Object nullValue, String formatString, IFormatProvider formatInfo) 处 在 System.Windows.Forms.ControlBindingsCollection.Add(String propertyName, Object dataSource, String dataMember) 处
从'System.DateTime'到'System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'的无效转换。
这是我们正在做的事情:
对象属性:
private System.DateTime? _dateTimeReceived;
public System.DateTime? DateTimeReceived
{
get
{
return this._dateTimeReceived;
}
set
{
this._dateTimeReceived = value;
this.OnChanged("DateTimeReceived", value); //Implements INotifyPropertyChanged and fires PropertyChanged event
}
}
控件属性:
private System.DateTime? _value;
[System.ComponentModel.Category("Behavior")]
[System.ComponentModel.Description("The current date value for this control")]
public new System.DateTime? Value
{
get
{
return this._value;
}
set
{
this._value = value;
}
}
在应用程序中,这是异常抛出的位置:
this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived");
正如您所看到的,对象的属性(this._object.DateTimeReceived)是System.DateTime?类型,控件的属性(this.dateReceived.Value)也是System.DateTime?类型。
为什么会导致InvalidCastException?我们该如何更正以使其正确绑定?
更新2009-10-29 14:26 CDT:
以下是堆栈跟踪:
在 System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) 处 在 System.DateTime.System.IConvertible.ToType(Type type, IFormatProvider provider) 处 在 System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) 处 在 System.Windows.Forms.Binding.FormatObject(Object value) 处 在 System.Windows.Forms.Binding.PushData(Boolean force) 处 在 System.Windows.Forms.Binding.UpdateIsBinding() 处 在 System.Windows.Forms.Binding.CheckBinding() 处 在 System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase) 处 在 System.Windows.Forms.ListManagerBindingsCollection.AddCore(Binding dataBinding) 处 在 System.Windows.Forms.BindingsCollection.Add(Binding binding) 处 在 System.Windows.Forms.BindingContext.UpdateBinding(BindingContext newBindingContext, Binding binding) 处 在 System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent value) 处 在 System.Windows.Forms.ControlBindingsCollection.AddCore(Binding dataBinding) 处 在 System.Windows.Forms.BindingsCollection.Add(Binding binding) 处 在 System.Windows.Forms.ControlBindingsCollection.Add(String propertyName, Object dataSource, String dataMember, Boolean formattingEnabled, DataSourceUpdateMode updateMode, Object nullValue, String formatString, IFormatProvider formatInfo) 处 在 System.Windows.Forms.ControlBindingsCollection.Add(String propertyName, Object dataSource, String dataMember) 处