【问题标题】:Change single datasource with multiple comboboxes使用多个组合框更改单个数据源
【发布时间】:2017-03-21 08:58:14
【问题描述】:

我目前正在为角色扮演游戏开发角色生成器。我有一个滚动能力分数的方法,我把它们放在一个数组中。我将数组转换为列表。然后我将所有组合框指向数据源。我将它们分别绑定,因此它们都不会更改为相同的数字。不幸的是,一旦选择了从列表中删除的数字,我遇到了很多问题。这是到目前为止的样子。任何帮助,将不胜感激。提前致谢。

List<int> abilityList = allAtribs.ToList();
        strCombo.DataSource = new BindingSource(abilityList, null);
        dexCombo.DataSource = new BindingSource(abilityList, null);
        conCombo.DataSource = new BindingSource(abilityList, null);
        intCombo.DataSource = new BindingSource(abilityList, null);
        wisCombo.DataSource = new BindingSource(abilityList, null);
        chaCombo.DataSource = new BindingSource(abilityList, null);

【问题讨论】:

    标签: c# combobox datasource


    【解决方案1】:

    创建一个BindingList&lt;int&gt;,而不是List&lt;int&gt;,并将其存储在表单成员中。

    表格:

    private BindingList<int> abilityList;
    

    初始化:

    // if allAttribs is int[], you can remove .ToList()
    abilityList = new BindingList<int>(allAtribs.ToList());
    strCombo.DataSource = new BindingSource(abilityList, null);
    dexCombo.DataSource = new BindingSource(abilityList, null);
    // etc....
    

    然后您可以简单地从abilityList 中删除一个值(或添加新值,更新现有值),更改将自动反映在所有组合框中。

    这是因为BindingList&lt;T&gt; 除了是IList&lt;T&gt; 还提供数据绑定感知组件识别的标准更改通知。

    【讨论】:

      猜你喜欢
      • 2018-10-11
      • 2023-03-08
      • 2018-08-28
      • 2012-09-01
      • 2016-07-30
      • 1970-01-01
      • 2013-09-07
      • 1970-01-01
      • 2011-05-19
      相关资源
      最近更新 更多