Iva*_*ono 8 c# data-binding winforms
我有2个DTO课程:
public class AddressDto
{
public string Street { get; set; }
public string City { get; set; }
public string PostCode { get: set: }
}
public class CustomerDto
{
public int Number{ get; set; }
public string Name { get; set; }
public AddressDto Address { get: set: }
public CustomerDto()
{
Address = new AddressDto();
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个带有绑定源的表单,它绑定到CustomerDto
.我还有一个带有地址字段的自定义控件.此自定义控件具有绑定到AddressDto
的绑定源控件的文本框正确绑定到地址属性.
该控件公开以下属性:
[Bindable(BindableSupport.Yes, BindingDirection.TwoWay)]
[Browsable(false)]
public object Address
{
get { return bindingSource.DataSource; }
set { bindingSource.DataSource = value; }
}
Run Code Online (Sandbox Code Playgroud)
在一台机器上我没有任何错误CheckBinding()
.但是在另一台机器上,当我尝试在运行时打开表单时,我得到了上述异常.在设计时,一切正常.
控件有3 TextBoxes
,设计器添加以下绑定:
this.bindingSource.AllowNew = true;
this.bindingSource.DataSource = typeof(AddressDto);
this.txtStreet.DataBindings.Add(new Binding("Text", this.bindingSource, "Street", true));
this.txtCity.DataBindings.Add(new Binding("Text", this.bindingSource, "City", true));
this.txtPostCode.DataBindings.Add(new Binding("Text", this.bindingSource, "PostCode", true));
Run Code Online (Sandbox Code Playgroud)
问题可以解决的任何想法?
我将代码更改为:
[Bindable(BindableSupport.Yes, BindingDirection.TwoWay)]
[Browsable(false)]
public object Address
{
get { return bindingSource.DataSource; }
set
{
if (value != null && value != System.DBNull.Value)
bindingSource.DataSource = value;
else
bindingSource.DataSource = typeof(AddressDto);
}
}
Run Code Online (Sandbox Code Playgroud)
价值是System.DBNull
.通过上述更改,不再抛出异常.
这解决了这个问题.但是,为什么值DBNull
仍然不明确,因为我使用纯POCO类作为我的绑定源的数据源.
归档时间: |
|
查看次数: |
3140 次 |
最近记录: |