列表框数据绑定无提示失败

时间:2013-04-25 19:39:29

标签: c# .net winforms

这种方法对我来说似乎有一半的时间。

我可以看到这些行在调试器中执行:

agencyListBox.DataBindings.Add(new Binding("DataSource", this.Data.Agencies, "AvailableAgencies"));
agencyListBox.DataBindings.Add(new Binding("SelectedItem", this.Data.Agencies, "SelectedAgency", false, DataSourceUpdateMode.OnPropertyChanged));

代理机构类看起来像这样:

public AgencyType SelectedAgency
{
    get
    {
        return _selected;
    }
    set
    {
        _selected = value;
        OnPropertyChanged("SelectedAgency");
    }
}

public List<AgencyType> AvailableAgencies
{
    get
    {
        return _availableList;
    }
    set
    {
        _availableList = value;
        OnPropertyChanged("AvailableAgencies");
    }
}

所以我在绑定中引用的字段确实存在。

DisplayMember设置为“Label”,在AgencyType类中定义:

public event PropertyChangedEventHandler PropertyChanged;

private string _label { get; set; }

public string Label
{
    get { return _label; }
    set
    {
        _label = value;
        OnPropertyChanged("Label");
    }
}

private string _identifier { get; set; }

public string Identifier
{
    get { return _identifier; }
    set
    {
        _identifier = value;
        OnPropertyChanged("Identifier");
    }
}

public AgencyType()
{
    Label = string.Empty;
    Identifier = string.Empty;
}

private void OnPropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

根据需要显示值。

但是当我更改选择时,data.Agencies.SelectedAgency为null!

有没有人有任何提示?

0 个答案:

没有答案