【问题标题】:WCF - list in ComboBoxWCF - 组合框中的列表
【发布时间】:2016-05-28 23:24:57
【问题描述】:

我想从列表的一个字段中获取国家/地区名称并将它们放入组合框:

    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;
    }

我认为我可以在没有 FirstOrDefault() 的情况下使用 LINQ,但在这种情况下我不能这样做。

 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);

    }

我尝试将这些信息像这样放在 ComboBox 中:

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

我只给出第一个国家或类似的国家:

comboBox1.Items.Add(response);

我什么也没得到。

我使用 WCF 的第一步!请谅解!

【问题讨论】:

    标签: c# winforms wcf


    【解决方案1】:

    所以,如果我正确理解了您的问题,您想在 ComboBox 中填充任何 response.OffersCountry 属性中包含的所有国家/地区,对吗?

    由于您提到您是 WPF 新手,我将跳过有关 MVVM and DataBinding 的部分,并向您展示一种可以使用您现在拥有的方式来完成的方法。

    首先,您需要从Offers 中“提取”所有国家/地区,最好只提取一次并按字母顺序排序。

    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
    

    我建议不要手动添加项目,而是通过设置 DataSource 属性一次分配所有项目。

    comboBox1.DataSource= countries;
    

    您需要确保 Items 为空,手动添加的项目和 DataSource 不能很好地协同工作。

    如果您想预先选择某个国家/地区(例如来自ThisOffer 的国家),您可以设置组合框的SelectedItem 属性:

    comboBox1.SelectedItem = response.ThisOffer.Country;
    

    【讨论】:

    • 但我不能将 List 用于我的数据 - _ 错误无法将类型 'System.Linq.IOrderedEnumerable' 隐式转换为 'System.Collections.Generic.List '。存在显式转换(您是否缺少演员表?)_并且我得到的信息是 ComboBox 不包含 ItemsSource 的定义,DataSource 是否相同?
    • @KlaudiaW。我更新了声明,你也需要调用ToList,至于ComboBox不包含ItemsSource的定义:我以为你使用的是WPF,我的错。是的,你应该可以使用DataSource,我会更新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多