Nullable< DateTime>へのバインド制御プロパティ
-
08-07-2019 - |
質問
「値」を持つカスタムコントロールがあります。 System.Nullable型のプロパティ(別名System.DateTime?)。 「受信済み」のオブジェクトがあります。同じタイプのプロパティ。コントロールをオブジェクトにバインドしようとすると、次の InvalidCastException がスローされます。
「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の更新:
スタックトレースは次のとおりです。
at System.Convert.DefaultToType(IConvertible value、Type targetType、 IFormatProviderプロバイダー)
at System.DateTime.System.IConvertible.ToType(タイプタイプ、IFormatProvider provider)
at System.Convert.ChangeType(Object value、Type conversionType、IFormatProviderプロバイダー)
at System.Windows.Forms.Binding.FormatObject(Object value)
at System.Windows.Forms.Binding.PushData(Boolean force)
at System.Windows.Forms.Binding.UpdateIsBinding()
で System.Windows.Forms.Binding.CheckBinding()
で System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase)
at System.Windows.Forms.ListManagerBindingsCollection.AddCore(Binding dataBinding)
at System.Windows.Forms.BindingsCollection.Add(Binding binding)
で System.Windows.Forms.BindingContext.UpdateBinding(BindingContext newBindingContext、Binding binding)
System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent 値)
at System.Windows.Forms.ControlBindingsCollection.AddCore(Binding dataBinding)
at System.Windows.Forms.BindingsCollection.Add(Binding binding)
で System.Windows.Forms.ControlBindingsCollection.Add(String propertyName、Object dataSource、String dataMember、Boolean formatEnabled、DataSourceUpdateMode updateMode、Object nullValue、 String formatString、IFormatProvider formatInfo)
at System.Windows.Forms.ControlBindingsCollection.Add(String propertyName、Object dataSource、String dataMember)
解決
私は同じことをしようとしていましたが、なんとかnullableにバインドされた実用的なサンプルコードを見つけることができました。 formatEnabledをtrueに設定すると機能しますが、falseの場合は無効なキャスト例外が発生します。
このように見えるコード:
this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived");
代わりに次のようになります。
this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived", true);
明らかに、古いデータバインディングコードでは型が正確に一致する必要がありますが、Microsoftは後で型を自動的に変換する機能を追加しました。ここから: http://msdn.microsoft.com/en-us/library/ aa480734.aspx
.NET Frameworkの以前のバージョンでは、BindingオブジェクトのFormatおよびParseイベントを使用して、型変換と書式設定を手動で実行する必要がありました。 FormattingEnabledプロパティを直接設定するか、ControlBindingsCollectionのAddメソッドにtrueを渡すことにより、Bindingオブジェクトで書式設定を有効にすることで、これを実行できるようになりました。