peetman
peetman

Reputation: 707

Add multiple list items to combobox

How do I get all the testName present in the ListForTesting into the combobox? I have tried combobox_testType.ItemsSource = ListForTesting.testName but it doesn't work.

public class ManoeuvresID
{
    public string manName { get; set; }
    public int manID { get; set; }
}

public class TestID
{
    public string testName { get; set; }
    public int ID { get; set; }
    public IList<ManoeuvresID> manoeuvres { get; set; }
}

    HardCodedDatabase.TestID TestObjet = new HardCodedDatabase.TestID();
    TestObjet.manoeuvres = new List<HardCodedDatabase.ManoeuvresID>();
    HardCodedDatabase.ManoeuvresID man = new HardCodedDatabase.ManoeuvresID();
    man.manName="man1";
    man.manID = 2;
    TestObjet.manoeuvres.Add(man);
    List<HardCodedDatabase.TestID> ListForTesting = new List<HardCodedDatabase.TestID>();
    TestObjet.testName = "Steady State";
    ListForTesting.Add(TestObjet);
    TestObjet = new HardCodedDatabase.TestID();
    TestObjet.testName = "test2";
    ListForTesting.Add(TestObjet);
    combobox_testType.ItemsSource = ListForTesting;

Upvotes: 1

Views: 4012

Answers (2)

MikeT
MikeT

Reputation: 5500

have you tried linq

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

another option would be to use the DisplayMemberPath

combobox_testType.ItemsSource = ListForTesting;
combobox_testType.DisplayMemberPath= "testName";

Upvotes: 1

Chris Cooper
Chris Cooper

Reputation: 389

All you need to is add you object items using Items.Add or Items.AddRange Example below

        foreach (var item in ListForTesting)
            comBox.Items.Add(item);

Add range

 comBox.Items.AddRange(ListForTesting);

Then you set, which properties within you class object, will be used to display and is you key

        comBox.DisplayMember = "testName";
        comBox.ValueMember = "TestId";

Upvotes: 3

Related Questions