[英]Binding to a Nullable<DateTime> control property
We have a custom control that has a "Value" property of type System.Nullable (aka System.DateTime?). 我们有一个自定义控件,它具有System.Nullable类型的“Value”属性(又名System.DateTime?)。 We have an object with a "Received" property of the same type.
我们有一个具有相同类型的“已接收”属性的对象。 When we try to bind the control to the object, the following InvalidCastException is thrown:
当我们尝试将控件绑定到对象时,抛出以下InvalidCastException :
Invalid cast from 'System.DateTime' to 'System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'. 从'System.DateTime'到'System.Nullable`1 [[System.DateTime,mscorlib,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]'的无效演员表。
Here is what we're doing: 以下是我们正在做的事情:
Object property: 对象属性:
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
}
}
Control property: 控制属性:
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;
}
}
In the application, here is where the exception is thrown: 在应用程序中,抛出异常的位置:
this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived");
As you can see, the object's property (this._object.DateTimeReceived) is a System.DateTime? 如您所见,对象的属性(this._object.DateTimeReceived)是System.DateTime? type and the control's property (this.dateReceived.Value) is a System.DateTime?
类型和控件的属性(this.dateReceived.Value)是一个System.DateTime? type.
类型。
Why would this cause an InvalidCastException ? 为什么会导致InvalidCastException ? And how can we correct this so that it binds correctly?
我们如何纠正这一点,使其正确绑定?
Update 2009-10-29 14:26 CDT: 更新2009-10-29 14:26 CDT:
Here is the stack trace: 这是堆栈跟踪:
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
在System.Convert.DefaultToType(IConvertible值,类型targetType,IFormatProvider提供程序)
at System.DateTime.System.IConvertible.ToType(Type type, IFormatProvider provider)在System.DateTime.System.IConvertible.ToType(Type type,IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)at System.Convert.ChangeType(Object value,Type conversionType,IFormatProvider provider)
at System.Windows.Forms.Binding.FormatObject(Object value)在System.Windows.Forms.Binding.FormatObject(对象值)
at System.Windows.Forms.Binding.PushData(Boolean force)在System.Windows.Forms.Binding.PushData(布尔力)
at System.Windows.Forms.Binding.UpdateIsBinding()在System.Windows.Forms.Binding.UpdateIsBinding()
at System.Windows.Forms.Binding.CheckBinding()在System.Windows.Forms.Binding.CheckBinding()
at System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase)在System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase)
at System.Windows.Forms.ListManagerBindingsCollection.AddCore(Binding dataBinding)在System.Windows.Forms.ListManagerBindingsCollection.AddCore(Binding dataBinding)
at System.Windows.Forms.BindingsCollection.Add(Binding binding)在System.Windows.Forms.BindingsCollection.Add(绑定绑定)
at System.Windows.Forms.BindingContext.UpdateBinding(BindingContext newBindingContext, Binding binding)在System.Windows.Forms.BindingContext.UpdateBinding(BindingContext newBindingContext,Binding binding)
at System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent value)在System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent值)
at System.Windows.Forms.ControlBindingsCollection.AddCore(Binding dataBinding)在System.Windows.Forms.ControlBindingsCollection.AddCore(Binding dataBinding)
at System.Windows.Forms.BindingsCollection.Add(Binding binding)在System.Windows.Forms.BindingsCollection.Add(绑定绑定)
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,Boolean formattingEnabled,DataSourceUpdateMode updateMode,Object nullValue,String formatString,IFormatProvider formatInfo)
at System.Windows.Forms.ControlBindingsCollection.Add(String propertyName, Object dataSource, String dataMember)在System.Windows.Forms.ControlBindingsCollection.Add(String propertyName,Object dataSource,String dataMember)
I was trying to do the same thing, and I managed to find some working sample code which bound to a nullable. 我试图做同样的事情,我设法找到一些绑定到可空的可用示例代码。 It turns out that if you set the formattingEnabled to true, it works, but if it's false, you get the invalid cast exception.
事实证明,如果你将formattingEnabled设置为true,它会起作用,但如果它为false,则会得到无效的强制转换异常。
So your code that looks like this: 所以你的代码看起来像这样:
this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived");
Should instead look like this: 应该看起来像这样:
this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived", true);
Apparently the old data binding code requires that the types match exactly, but Microsoft later added the ability to automatically convert types for you. 显然,旧的数据绑定代码要求类型完全匹配,但Microsoft后来添加了为您自动转换类型的功能。 From here: http://msdn.microsoft.com/en-us/library/aa480734.aspx
从这里: 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.
在早期版本的.NET Framework中,您必须使用Binding对象的Format和Parse事件手动执行类型转换和格式化。 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.
您现在可以通过在Binding对象上启用格式化来实现此目的,方法是直接设置FormattingEnabled属性或将true传递给ControlBindingsCollection的Add方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.