绑定到可空的<DateTime>控件属性

8
我们有一个自定义控件,具有类型为System.Nullable(也称为System.DateTime?)的“Value”属性。我们有一个具有相同类型的“Received”属性的对象。当我们试图将控件绑定到该对象时,会抛出以下无效强制转换异常:
从'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) 处

你的异常的完整堆栈跟踪是什么(请在VS调试设置中禁用“只查看我的代码”,以便显示.NET Framework本身的堆栈帧)? - Pavel Minaev
1个回答

14

我试图做同样的事情,并且我找到了一些可以绑定到可空类型的工作示例代码。事实证明,如果将 formattingEnabled 设置为 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事件。现在,您可以通过在Binding对象上启用格式化,直接设置FormattingEnabled属性或将true传递给ControlBindingsCollection的Add方法来完成此操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接