Windows窗体上的数据绑定在对象图上可能具有null属性? [英] Databinding in windows forms on an object graph with possible null properties?

查看:76
本文介绍了Windows窗体上的数据绑定在对象图上可能具有null属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的对象图:

If I have an object graph like this:

class Company {
  public Address MainAddress {...}
}

class Address {
  public string City { ... }
}

Company c = new Company();
c.MainAddress = new Address();
c.MainAddress.City = "Stockholm";

并将数据绑定到使用以下控件的控件:

and databind to a control using:

textBox1.DataBinding.Add( "Text", c, "MainAddress.City" );

一切都很好,但是如果我绑定到:

Everything is fine, but If I bind to:

Company c2 = new Company();

c2 自崩溃以来使用的相同语法 MainAddress 属性为空。我想知道是否有一个自定义的Binding类,可以在此处为所有可能的路径设置侦听器,并在/如果我稍后在应用程序中设置 MainAddress 属性。

c2 using the same syntax it crashes since the MainAddress property is null. I wonder if there is a custom Binding class that can set up listeners for all the possible paths here and bind to the actual object dynamically when/if I sometime later in the application set the MainAddress property.

推荐答案

您应更改属性MainAddress的吸气剂以执行此空检查,并返回空MainAddress

You should change the getter of the property MainAddress to do this null check, and return an "empty MainAddress"

这篇关于Windows窗体上的数据绑定在对象图上可能具有null属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆