绑定到Nullable <DateTime>控件属性

Ste*_*ing 8 c# data-binding datetime nullable winforms

我们有一个自定义控件,它具有System.Nullable类型的"Value"属性(又名System.DateTime?).我们有一个具有相同类型的"已接收"属性的对象.当我们尝试将控件绑定到对象时,抛出以下InvalidCastException:

从 '的System.DateTime' 无效转换到 'System.Nullable`1 [[System.DateTime的,mscorlib程序,版本= 2.0.0.0,文化=中性公钥= 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
    }
}
Run Code Online (Sandbox Code Playgroud)

控制属性:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在应用程序中,抛出异常的位置:

this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived");
Run Code Online (Sandbox Code Playgroud)

如您所见,对象的属性(this._object.DateTimeReceived)是System.DateTime?类型和控件的属性(this.dateReceived.Value)是一个System.DateTime?类型.

为什么会导致InvalidCastException?我们如何纠正这一点,使其正确绑定?

更新2009-10-29 14:26 CDT:

这是堆栈跟踪:

在System.Convert.DefaultToType(IConvertible值,类型TARGETTYPE,的IFormatProvider提供商)
在System.DateTime.System.IConvertible.ToType(类型类型,的IFormatProvider提供商)
在System.Convert.ChangeType(对象值,类型conversionType,的IFormatProvider提供商)
在System.Windows.Forms.Binding.FormatObject(对象的值)
在System.Windows.Forms.Binding.PushData(布尔力)
在System.Windows.Forms.Binding.UpdateIsBinding()
在System.Windows.Forms.Binding.CheckBinding( )
在System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase)
在System.Windows.Forms.ListManagerBindingsCollection.AddCore(绑定数据绑定)
在System.Windows.Forms.BindingsCollection.Add(绑定结合)
在System.Windows.Forms的. BindingContext.UpdateBinding(的BindingContext newBindingContext,绑定结合)
在System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent值)
在System.Windows.Forms.ControlBindingsCollection.AddCore(绑定数据绑定)
在System.Windows.Forms.B indingsCollection.Add(绑定结合)
在System.Windows.Forms.ControlBindingsCollection.Add(字符串propertyName的,DataSource对象,字符串数据成员,布尔formattingEnabled,DataSourceUpdateMode的UpdateMode,对象NullValue属性,字符串formatString的,的IFormatProvider formatInfo)
在System.Windows.Forms.ControlBindingsCollection .Add(String propertyName,Object dataSource,String dataMember)

Bry*_*ner 13

我试图做同样的事情,我设法找到一些绑定到可空的可用示例代码.事实证明,如果你将formattingEnabled设置为true,它会起作用,但如果它为false,则会得到无效的强制转换异常.

所以你的代码看起来像这样:

this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived");
Run Code Online (Sandbox Code Playgroud)

应该看起来像这样:

this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived", true);
Run Code Online (Sandbox Code Playgroud)

显然,旧的数据绑定代码要求类型完全匹配,但Microsoft后来添加了为您自动转换类型的功能.从这里:http://msdn.microsoft.com/en-us/library/aa480734.aspx

在早期版本的.NET Framework中,您必须使用Binding对象的Format和Parse事件手动执行类型转换和格式化.您现在可以通过在Binding对象上启用格式化来实现此目的,方法是直接设置FormattingEnabled属性或将true传递给ControlBindingsCollection的Add方法.