绑定到Nullable <datetime>控件属性</datetime>

时间:2009-10-28 21:42:21

标签: c# winforms data-binding datetime nullable

我们有一个自定义控件,它具有System.Nullable类型的“Value”属性(又名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:

这是堆栈跟踪:

  

在System.Convert.DefaultToType(IConvertible值,Type targetType,   IFormatProvider提供者)
在   System.DateTime.System.IConvertible.ToType(Type type,IFormatProvider   提供者)
在System.Convert.ChangeType(对象值,类型   conversionType,IFormatProvider provider)
在   System.Windows.Forms.Binding.FormatObject(Object value)
在   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(绑定   dataBinding)
在   System.Windows.Forms.BindingsCollection.Add(Binding binding)
在   System.Windows.Forms.BindingContext.UpdateBinding(BindingContext中   newBindingContext,绑定绑定)
在   System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent   价值)   System.Windows.Forms.ControlBindingsCollection.AddCore(绑定   dataBinding)
在   System.Windows.Forms.BindingsCollection.Add(Binding binding)
在   System.Windows.Forms.ControlBindingsCollection.Add(字符串   propertyName,Object dataSource,String dataMember,Boolean   formattingEnabled,DataSourceUpdateMode updateMode,Object nullValue,   String formatString,IFormatProvider formatInfo)
在   System.Windows.Forms.ControlBindingsCollection.Add(字符串   propertyName,Object dataSource,String dataMember)

1 个答案:

答案 0 :(得分:13)

我试图做同样的事情,我设法找到一些绑定到可空的可用示例代码。事实证明,如果你将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方法。

相关问题