将ComboBox与Dictionary的值链接

时间:2018-11-30 17:20:05

标签: c# wpf dictionary combobox

我想在一个带变量的for循环中调用3个列表,并得到类似这样的信息:

for (int i = 1; i <= 3; i++)
{
    Combobox MyList(i) = new Combobox();
    MyList(i).Width = 120;
    MyList(i).Height = 30;
    etc...
}

无法以这种方式完成,因此我寻找其他方式,发现可以使用字典来完成。但是然后我无法用字典的值(即列表)填充组合框。

我做了这样的事情:

namespace GenDet
{
    public partial class NewDetail : Page
    {
    int Nbe = 0;
    List<String> MyElements = new List<String>();
    List<String> MyElement1 = new List<String>();
    List<String> MyElement2 = new List<String>();
    List<String> MyElement3 = new List<String>();

    Dictionary<int, List<string>> MyDictionary = new Dictionary<int, List<string>>();

    public NewDetail()
    {
        InitializeComponent();
        MyDictionary.Add(1, MyElement1);
        MyDictionary.Add(2, MyElement2);
        MyDictionary.Add(3, MyElement3);
    }

    private void OnClick1(object sender, RoutedEventArgs e)
    {

        string MyValue = MyList.SelectedValue.ToString();
        if (Nbe == 3)
        {
            MessageBox.Show("Max elements reached", "Alert", MessageBoxButton.OK, MessageBoxImage.Information);
        } else
        {
            AddList(Nbe, MyValue, MyElements);
        }

    }
    private void AddList (int Nbe, string MyValue, List<string> MyElements)
    {
        List<String> MyNewList = new List<String>();
        if (Nbe==0 || (MyElements[Nbe-1] != MyValue && (MyElements[Nbe-1] == "Mur" || MyValue == "Mur"))) 
        {
            //String MyListName = "MyNewList" + Nbe.ToString();
            MyElements.Add(MyValue);
            Nbe += 1;
        }
        for (int i = 1; i <= Nbe; i++)
        {
           //MyComboBox[i] = MyDictionary value for key = i;
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

实际上,可以按照您初次编写的方式来完成,但是:

1。首先创建您的comboBox数组(employeeArray.sort(sortTwoArrays);

2.C3中的索引使用ComboBox[]而不是[]

3。索引从0开始,因此从0循环到2

()