Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# listbox数据绑定以静默方式失败_C#_.net_Winforms - Fatal编程技术网

C# listbox数据绑定以静默方式失败

C# listbox数据绑定以静默方式失败,c#,.net,winforms,C#,.net,Winforms,这种方法似乎有一半的时间对我有效 我可以看到这些行在调试器中执行: agencyListBox.DataBindings.Add(new Binding("DataSource", this.Data.Agencies, "AvailableAgencies")); agencyListBox.DataBindings.Add(new Binding("SelectedItem", this.Data.Agencies, "SelectedAgency", false, DataSourceUp

这种方法似乎有一半的时间对我有效

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

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");
    }
}
这些值将根据需要显示

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


有人有什么建议吗?

为什么你会有一套可用资源。这不是说你可以在UI中更改列表?@Blam:列表是列表框中的选项列表。列表之间没有变化;它在列表中的元素之间变化。同样的概念也适用于我UI中的其他地方。如果选项列表没有改变,为什么它有一个集合。您是否尝试删除AvailableAgencies上的设置?@Blam:当我最初填充它时(从其他地方)它会发生变化。可用代理按需要显示,但所选代理始终保持为空。看起来很简单!
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));
    }
}