【问题标题】:Binding separate list into one listbox将单独的列表绑定到一个列表框中
【发布时间】:2014-01-27 01:42:44
【问题描述】:

我想将我的列表框与我从 json Web 服务获得的单独列表数据绑定,所以它看起来像这样:

public List<Phone> phone { get; set; }
public List<Fax> fax { get; set; }
public List<Email> email { get; set; }
public List<Website> website { get; set; }

我想将所有列表绑定到一个列表框中,所以我的列表框将在一个项目中包含电话、传真、电子邮件和网站...如何做到这一点?

编辑:我想到的格式示例

电话:+04532534534

传真:+5234523453

电子邮件:user@user.com

网站:www.user.com

编辑:到目前为止,我的方法是将分隔列表放入我的主视图模型中的分隔属性中

RootObjectDetail result = JsonConvert.DeserializeObject<RootObjectDetail>(e.Result);
if (result.contacts.email != null)
    hereRestEmail = new ObservableCollection<Email>(result.contacts.email);
if (result.contacts.phone != null)
    hereRestPhone = new ObservableCollection<Phone>(result.contacts.phone);
if (result.media.reviews.items != null)
    hereRestReview = new ObservableCollection<ItemReview>(result.media.reviews.items);
if (result.media.images.items != null)
    hereRestImage = new ObservableCollection<ItemImage>(result.media.images.items);

但我只是不知道如何将它绑定到我的列表框数据上下文中,因为我只能绑定其中一个属性

 <telerikPrimitives:RadDataBoundListBox
   x:Name="ContactListBox"
   ItemsSource="{Binding hereRestPhone}">
    <telerikPrimitives:RadDataBoundListBox.ItemTemplate>
       <DataTemplate>

          <TextBlock Text="{Binding LocalizedResources.phone,
                      Mode=OneWay, Source={StaticResource LocalizedStrings}}" 
                       Style="{StaticResource PhoneTextLargeStyle}"/>
          <TextBlock Grid.Column="1" Text="{Binding value}" 
                       Style="{StaticResource PhoneTextLargeStyle}" />

       </DataTemplate>
     </telerikPrimitives:RadDataBoundListBox.ItemTemplate>

它只能显示电话,我仍然不知道如何将传真,电子邮件和网站绑定到这个列表框

【问题讨论】:

  • 我已经将它编辑成我为我的列表考虑的格式,所以它就像我列表中的每个项目一样......示例是列表项目 1 和项目 2 和 3它会像那样,但电话、传真、电子邮件、网站的价值不同

标签: c# xaml mvvm listbox windows-phone


【解决方案1】:

在不知道你的类内部是什么样子的情况下,我只能猜测:

 var allValues =
     phone.Select(x => new { Type = "phone", Value = x.PhoneNumber })
          .Concat(fax.Select(x => new { Type = "fax", Value = x.FaxNumber }))
          .Concat(email.Select(x => new { Type = "email", Value = x.EmailAddr }))
          .Concat(website.Select(x => new { Type = "website", Value = x.URL }))
          .ToList();

您最终会得到一个(匿名类型)列表,其中包含“类型”和“值”。

我还没有对此进行测试,但是您应该能够在 XAML 中设置模板,绑定到“类型”和“值”,并让它们像您在上面指出的那样显示。

【讨论】:

  • 哇,这是我以前从未见过的语法...介意给我有关此语法的参考吗?
  • hmmm 我认为你为 concat 语句放置了错误的链接,因为它让我跳转到与 LINQ 相同的链接...,并且在阅读了一段时间后,LINQ 是某种数据库/sql,所以我不不明白为什么这可以用来解决我的问题..也许你可以看到我的帖子(已经编辑)更详细
猜你喜欢
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 2012-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-08
相关资源
最近更新 更多