如何在c#中将多个项目源添加到列表框

时间:2014-01-22 16:21:04

标签: c# json windows listbox

private void w_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{

    if (!string.IsNullOrEmpty(e.Result))
    {
        //Parse JSON result as POCO 
        var root1 = JsonConvert.DeserializeObject<RootObject>(e.Result);
        var root2 = JsonConvert.DeserializeObject<Headline>(e.Result);
        lstShow.ItemsSource = root1.headlines;
       // lstShow.ItemsSource = root2.images;
    }
}

我正在尝试将多个项目源添加到我的列表框'lstShow'。这是我的xaml:

<ListBox x:Name="lstShow" FontFamily="Arial Black" VerticalAlignment="Center" Margin="-6,0,0,-26" Height="610" RenderTransformOrigin="0.5,0.5" Background="{x:Null}" Opacity="0.8">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Button  Style="{StaticResource ButtonStyle1}" Tag="{Binding News}" Width="450" Height="Auto" Background="Black" BorderBrush="Transparent" FontWeight="Bold" FontSize="23" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,5" Opacity="0.95" Click="news_click" Foreground="White">
                    <StackPanel>
                        <Image Source="{Binding url}" Height="100" Width="200"/>
                    <TextBlock  TextWrapping="Wrap" FontFamily="Segoe WP Black" Foreground="White" FontSize="18" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TextAlignment="Left" Width="350" Height="150">
                        <Run FontSize="23" Text="{Binding headline}" />
                        <LineBreak/>
                        <Run Text="{Binding description}" FontWeight="Normal" FontSize="16" FontFamily="Segoe WP SemiLight"/>
                        </TextBlock>
                    </StackPanel>
                </Button>
              </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>

问题是我无法将多个项目源添加到我的列表框中。我想将root1和root2添加到我的列表框中。请帮忙,谢谢

 public class Headline
{
public string headline { get; set; }
public List<object> keywords { get; set; }
public string lastModified { get; set; }
public bool premium { get; set; }
public string mobileStory { get; set; }
public Links links { get; set; }
public string type { get; set; }
public List<object> related { get; set; }
public int id { get; set; }
public string story { get; set; }
public string title { get; set; }
public string linkText { get; set; }
public string byline { get; set; }
public string description { get; set; }
public List<object> images { get; set; }
public List<Category> categories { get; set; }
public string published { get; set; }
public List<object> video { get; set; }

}

public class RootObject
{
public string timestamp { get; set; }
public int resultsOffset { get; set; }
public string status { get; set; }
public int resultsLimit { get; set; }
public int resultsCount { get; set; }
public List<Headline> headlines { get; set; }

}

我想从RootObject添加列表'标题',并将标题中的'类别'列入列表框。

1 个答案:

答案 0 :(得分:0)

你没有显示Category类,所以我假装它看起来像这样:

public class Category
{
    public string Name { get; set; }
    public int Id { get; set; }
}

您可以从标题中选择所需的所有字段,然后展开类别列表:

var ro = root1.headlines.Select(x => new
         {
             x.byline,
             x.description,
             x.lastModified,
             ...
             ...
             categories = string.Join(", ", x.categories.Select(y => y.Name))
         }).ToList();