如何操作到ItemsSource的输出?

时间:2014-02-21 15:48:50

标签: c# .net xaml windows-phone-8 windows-phone

这个Previous Question 我也能够显示输出。 我在stackoverflow上阅读了很多帖子,但是找不到合适的解决方案:

如何操作输出ItemsSource,因为:

{"order_id":"12345678","itemList":["235724","203224","222224","222324","230021"],"amount":["65","50","10","25","42"]}

数字235724也在IMG网址中使用,需要变成类似于“235724 X 65”的可选列表框。

解决方案是:

ObservableCollection<CreateItem> pitems = new ObservableCollection<CreateItem>();

        for (int i = 0; i < rootObject.itemList.Count; i++)
        {
            var itemsku = rootObject.itemList[i];
            var amount = rootObject.amount[i];
            pitems.Add(new CreateItem() { pTitle = itemsku + " X " + amount , pImage = new BitmapImage(new Uri(string.Format("http://image.mgam79.nl/indb/{0}.jpg", itemsku)))});   
        }

        MyListBox.ItemsSource = pitems;

    public class CreateItem
    {
        public string pTitle { get; set; }
        public ImageSource pImage { get; set; }
    }

1 个答案:

答案 0 :(得分:1)

在RootObject.cs中创建一个新属性:

    public List<string> ImageUrls { get; set;}

在cs:

        var result = "{'order_id':'12345678','itemList':['235724','203224','222224','222324','230021'],'amount':['65','50','10','25','42']}";
        var rootObject = JsonConvert.DeserializeObject<RootObject>(result);

        var imageUrls = new List<string>();

        foreach (var item in rootObject.itemList)
        {
            foreach (var amount in rootObject.amount)
            {
                imageUrls.Add(item + " X " + amount);
            }
            break;
        }

        rootObject.ImageUrls = imageUrls;

        var items = new List<RootObject>
        {
            rootObject
        };

        MyListBox.ItemsSource = items;
    }

在xaml:

<ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical">
                            <TextBlock>
                                <TextBlock.Inlines>
                                    <Run Text="order_id:" />
                                    <Run Text="{Binding order_id}" />
                                </TextBlock.Inlines>
                            </TextBlock>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="ItemList: " />
                                <ItemsControl ItemsSource="{Binding itemList}" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Amount: " />
                                <ItemsControl ItemsSource="{Binding amount}" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="ImageUrls: " />
                                <ItemsControl ItemsSource="{Binding ImageUrls}" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>