Quicki
Quicki

Reputation: 401

WCF - list in ComboBox

I'd like to get country's names form one field of list and put them to the comboBox:

    public TravelAgencyResponse GetInformation(TravelAgencyRequest request)
    {
        TravelAgencyResponse response = new TravelAgencyResponse();
       // response.Offers = new OfferDto();
       response.Offers = new List<DataTransferObjects.OfferDto>();            
       response.Offers.Add(new DataTransferObjects.OfferDto()
        {
            IdOffer = 0,
            KindOfAccommodation = "Hotel",
            Country = "Spain",
        });

        response.Offers.Add(new DataTransferObjects.OfferDto()
        {
             IdOffer = 1,
             KindOfAccommodation = "Hotel",
             Country = "Italy",
         });

       response.ThisOffer = (from offer in response.Offers
                              where offer.Country == request.Country
                              select offer).FirstOrDefault();
        return response;
    }

I thought that I can use LINQ without FirstOrDefault() but I can't do that in this situation.

 private void button1_Click(object sender, EventArgs e)
    {
        Uri baseAddr = new Uri("http://localhost:1232/TravelAgencyService/SimpleTravelAgencyService/");
        ChannelFactory<ITravelAgencyService> factory = new ChannelFactory<ITravelAgencyService>(new WSHttpBinding(),
            new EndpointAddress(baseAddr));
        ITravelAgencyService proxy = factory.CreateChannel();
        var response = proxy.GetInformation(
            new TravelAgencyService.Messages.TravelAgencyRequest()
            {
                Country = textBox1.Text
            });

        comboBox1.Items.Add(response.ThisOffer.Country);
        listBox1.Items.Add(response.ThisOffer.Country);

    }

I tried put these information in ComboBox like that:

 comboBox1.Items.Add(response.ThisOffer.Country);

and I give only the first country or like that:

comboBox1.Items.Add(response);

and I don't get anything.

my first steps with WCF! Be understanding, please!

Upvotes: 1

Views: 240

Answers (1)

Manfred Radlwimmer
Manfred Radlwimmer

Reputation: 13394

So if I understand your question correctly, you want to fill a ComboBox with all the countries that are contained in any response.Offers's Country property, correct?

Since you mentioned you are new to WPF, I'll skip the part about MVVM and DataBinding and show you a way it can be done with what you have now.

First you need to "extract" all the countries from the Offers, preferably only once and sorted alphabetically.

List<string> countries = response.Offers
    .Select(o => o.Country) // We only need the "Country" of the offer
    .Distinct()             // Every country only once
    .OrderBy(c => c)        // Sort by name
    .ToList();              // make a List<string> out of it

Instead of adding items manually, I'd recommend assigning all of them at once, by setting the DataSource property.

comboBox1.DataSource= countries;

You need to make sure however that Items is empty, manually added items and DataSource do not work well together.

If you want to pre-select a certain country (e.g. the one from ThisOffer) you can set the SelectedItem property of the ComboBox:

comboBox1.SelectedItem = response.ThisOffer.Country;

Upvotes: 1

Related Questions