c# - 绑定(bind)到 Nullable<DateTime> 控件属性

标签 c# winforms data-binding datetime nullable

我们有一个自定义控件,它有一个类型为 System.Nullable(又名 System.DateTime?)的“Value”属性。我们有一个具有相同类型的“已接收”属性的对象。当我们尝试将控件绑定(bind)到对象时,将抛出以下 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?我们如何纠正它以使其正确绑定(bind)?

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

这是堆栈跟踪:

at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at System.DateTime.System.IConvertible.ToType(Type type, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Windows.Forms.Binding.FormatObject(Object value)
at System.Windows.Forms.Binding.PushData(Boolean force)
at System.Windows.Forms.Binding.UpdateIsBinding()
at System.Windows.Forms.Binding.CheckBinding()
at System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase)
at System.Windows.Forms.ListManagerBindingsCollection.AddCore(Binding dataBinding)
at System.Windows.Forms.BindingsCollection.Add(Binding binding)
at System.Windows.Forms.BindingContext.UpdateBinding(BindingContext newBindingContext, Binding binding)
at System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent value)
at System.Windows.Forms.ControlBindingsCollection.AddCore(Binding dataBinding)
at System.Windows.Forms.BindingsCollection.Add(Binding binding)
at System.Windows.Forms.ControlBindingsCollection.Add(String propertyName, Object dataSource, String dataMember, Boolean formattingEnabled, DataSourceUpdateMode updateMode, Object nullValue, String formatString, IFormatProvider formatInfo)
at System.Windows.Forms.ControlBindingsCollection.Add(String propertyName, Object dataSource, String dataMember)

最佳答案

我试图做同样的事情,并且我设法找到了一些绑定(bind)到可为 nullable 的工作示例代码。事实证明,如果将 formattingEnabled 设置为 true,它会起作用,但如果它为 false,则会出现无效转换异常。

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

this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived");

应该看起来像这样:

this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived", true);

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

In earlier versions of the .NET Framework you had to manually perform the type conversions and formatting using the Format and Parse events of the Binding object. You can now do this by enabling formatting on the Binding object, either by setting the FormattingEnabled property directly or passing true to the Add method of the ControlBindingsCollection.

关于c# - 绑定(bind)到 Nullable<DateTime> 控件属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1640306/

相关文章:

c# - 制作 NxN tic tac toe GUI wpf c#

c# - 调试时,如何在枚举中的任意位置启动 foreach 循环?

c# 如何在 DataGridView 的特定列中强制使用大写?

c# - 从模板向 C# WinForms 应用程序添加新选项卡?

winforms - XtraGrid 在更新其数据源后不刷新

azure - 从触发 blob 触发器的文件中获取元数据 Azure 函数

c# - XNA Game Studio 在 2013 年过时了吗?

c# - 为什么要使用 Create 方法而不是使用 "new"?

winforms - 在 PowerShell 中向 SSH session 发送命令并在 TextBox 中接收输出

android - Spinner 2 方式数据绑定(bind)