mmeulstee
mmeulstee

Reputation: 45

How to manipulate the output to a ItemsSource?

With this Previous Question i was able too manage to show the output. I've reading alot of posts here on stackoverflow, but cant find the right solution for the next:

How to manipulate the output too ItemsSource, because:

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

The number 235724 is also used within a IMG url and need to become like "235724 X 65" for a selectable listbox.

Solution was:

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

Upvotes: 1

Views: 104

Answers (1)

ROMAN
ROMAN

Reputation: 1576

Create in RootObject.cs a new property:

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

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

In 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>

Upvotes: 1

Related Questions