C# 绑定到可为空的<;日期时间>;控制属性

C# 绑定到可为空的<;日期时间>;控制属性,c#,winforms,data-binding,datetime,nullable,C#,Winforms,Data Binding,Datetime,Nullable,我们有一个自定义控件,它具有System.Nullable(又称System.DateTime?)类型的“Value”属性。我们有一个具有相同类型的“Received”属性的对象。当我们尝试将控件绑定到对象时,会引发以下InvalidCastException: this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived"); 从'System.DateTime'到'System.Nullable'1[

我们有一个自定义控件,它具有System.Nullable(又称System.DateTime?)类型的“Value”属性。我们有一个具有相同类型的“Received”属性的对象。当我们尝试将控件绑定到对象时,会引发以下InvalidCastException:

this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived");
从'System.DateTime'到'System.Nullable'1[[System.DateTime,mscorlib,版本=2.0.0.0,区域性=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;
    }
}
控制属性:

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.\u object.DateTimeReceived)是System.DateTime?类型,控件的属性(this.dateReceived.Value)是System.DateTime?类型

为什么这会导致一个无效的例外?我们怎样才能纠正这一点,使其正确绑定

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

以下是堆栈跟踪:

在System.Convert.DefaultToType(IConvertible值,键入targetType, IFormatProvider(提供程序)
System.DateTime.System.IConvertible.ToType(类型类型,IFormatProvider 提供程序)
位于System.Convert.ChangeType(对象值,类型 转换类型,IFormatProvider提供程序)
System.Windows.Forms.Binding.FormatObject(对象值)
位于 System.Windows.Forms.Binding.PushData(布尔力)
System.Windows.Forms.Binding.UpdatesBinding()位于 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 新绑定上下文,绑定绑定)
System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent 值)
在 System.Windows.Forms.ControlBindingsCollection.AddCore(绑定 数据绑定)
在 System.Windows.Forms.BindingsCollection.Add(绑定绑定)
位于 System.Windows.Forms.ControlBindingsCollection.Add(字符串 propertyName,对象数据源,字符串数据成员,布尔值 已启用格式化、DataSourceUpdateMode updateMode、对象nullValue、, 字符串格式字符串,IFormatProvider formatInfo)
System.Windows.Forms.ControlBindingsCollection.Add(字符串 propertyName、对象数据源、字符串数据成员)


我试着做同样的事情,并且我设法找到了一些绑定到可为null的。事实证明,如果将formattingEnabled设置为true,则可以正常工作,但如果设置为false,则会出现无效强制转换异常

您的代码如下所示:

this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived");
this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived", true);
应该是这样的:

this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived");
this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived", true);
显然,旧的数据绑定代码要求类型完全匹配,但Microsoft后来添加了自动转换类型的功能。从这里开始:

在早期版本的.NET Framework中,您必须使用绑定对象的格式和解析事件手动执行类型转换和格式设置。现在可以通过在绑定对象上启用格式化来实现这一点,可以直接设置FormattingEnabled属性,也可以将true传递给ControlBindingsCollection的Add方法


异常的完整堆栈跟踪是什么(请在VS调试设置中禁用“仅我的代码”,以便显示.NET Framework本身的堆栈帧)?