C# - 如何从自定义类中获取相关的组合框值

时间:2021-11-06 13:46:42

I have a list ListForTesting of type TestID.

我有一个类型为TestID的ListForTesting列表。

 List<HelpClass.TestID> ListForTesting;

    //Populating the list
    HelpClass.TestID TestObject = new HelpClass.TestID();
    .
    .
    //data here
    .
    .
    TestObject.Manoeuvres = new List<HelpClass.ManoeuvresID>();
    HelpClass.ManoeuvresID Manoeuvre = new HelpClass.ManoeuvresID();
    .
    .
    //more data here
    .
    .
TestObject.Manoeuvres.Add(Manoeuvre);
ListForTesting.Add(TestObject);

I am using that list to get a dropdown list in a combobox

我正在使用该列表来获取组合框中的下拉列表

combobox_testType.ItemsSource = ListForTesting.Select(t => t.testName);

Then I have another combobox which is dependent on the first one and I would like to have something like the following.

然后我有另一个组合框,它依赖于第一个组合框,我希望有类似下面这样的东西。

combobox_manouevre.ItemsSource = ListForTesting[selectedindexfromfirstcombobox].Manoeuvres.Select(t => t.manName);

3 个解决方案

#1


0  

I think you could do :

我想你可以这样做:

combobox1.ItemsSource = ListForTesting;
combobox1.DisplayMember= "testName";
combobox1.ValueMember= "id";

and in the selectedChanged event of combobox1 :

在combobox1的selectedChanged事件中:

combobox2.ItemsSource = ListForTesting.Where(x => x.id == combobox1.SelectedValue).Manoeuvres;
combobox2.DisplayMember= "manName";
combobox2.ValueMember= "idManoeuvre";

#2


0  

I am not sure if this is the most appropriate answer but it did work in the second combobox.

我不确定这是否是最合适的答案,但它确实在第二个组合框中有效。

combobox_manouevre.ItemsSource = ListForTesting.Where(x => x.testName == combobox_testType.SelectedValue.ToString()).SelectMany(l=>l.Manoeuvres).Select(o=>o.manName);

combobox_manouevre.ItemsSource = ListForTesting.Where(x => x.testName == combobox_testType.SelectedValue.ToString())。SelectMany(l => l.Manoeuvres).Select(o => o.manName);

#3


0  

For more visibility in your code you can do this :

为了提高代码的可见性,您可以执行以下操作:

combobox_manouevre.ItemsSource = ListForTesting.Where(x => x.testName == combobox_testType.SelectedValue.ToString()).FirstOrDefault().Manoeuvres;
combobox_manouevre.DisplayMember = "manName";
combobox_manouevre.ValueMember = "yourIdManoeuvre";

It's more safe to use the DisplayMember and ValueMember for separating the displayed value in your combobox and the value (id)..

使用DisplayMember和ValueMember分离组合框中的显示值和值(id)更安全。

For example :

例如 :

List of manœuvre :

manœuvre列表:

id= 1 name= "manoeuvre1"  
id= 2 name= "manoeuvre1" // (same name that id=1)  
id= 3 name= "manoeuvre2"

if you select the second "manoeuvre1", the id will be 2 and you will work with this id (no chance of being wrong)

如果您选择第二个“manoeuvre1”,则id将为2,您将使用此ID(没有错误的机会)

Same thing for your list ListForTesting

列表ListForTesting也是如此

#1


0  

I think you could do :

我想你可以这样做:

combobox1.ItemsSource = ListForTesting;
combobox1.DisplayMember= "testName";
combobox1.ValueMember= "id";

and in the selectedChanged event of combobox1 :

在combobox1的selectedChanged事件中:

combobox2.ItemsSource = ListForTesting.Where(x => x.id == combobox1.SelectedValue).Manoeuvres;
combobox2.DisplayMember= "manName";
combobox2.ValueMember= "idManoeuvre";

#2


0  

I am not sure if this is the most appropriate answer but it did work in the second combobox.

我不确定这是否是最合适的答案,但它确实在第二个组合框中有效。

combobox_manouevre.ItemsSource = ListForTesting.Where(x => x.testName == combobox_testType.SelectedValue.ToString()).SelectMany(l=>l.Manoeuvres).Select(o=>o.manName);

combobox_manouevre.ItemsSource = ListForTesting.Where(x => x.testName == combobox_testType.SelectedValue.ToString())。SelectMany(l => l.Manoeuvres).Select(o => o.manName);

#3


0  

For more visibility in your code you can do this :

为了提高代码的可见性,您可以执行以下操作:

combobox_manouevre.ItemsSource = ListForTesting.Where(x => x.testName == combobox_testType.SelectedValue.ToString()).FirstOrDefault().Manoeuvres;
combobox_manouevre.DisplayMember = "manName";
combobox_manouevre.ValueMember = "yourIdManoeuvre";

It's more safe to use the DisplayMember and ValueMember for separating the displayed value in your combobox and the value (id)..

使用DisplayMember和ValueMember分离组合框中的显示值和值(id)更安全。

For example :

例如 :

List of manœuvre :

manœuvre列表:

id= 1 name= "manoeuvre1"  
id= 2 name= "manoeuvre1" // (same name that id=1)  
id= 3 name= "manoeuvre2"

if you select the second "manoeuvre1", the id will be 2 and you will work with this id (no chance of being wrong)

如果您选择第二个“manoeuvre1”,则id将为2,您将使用此ID(没有错误的机会)

Same thing for your list ListForTesting

列表ListForTesting也是如此