AKD
AKD

Reputation: 3966

bind data into a dynamically created grid in windows phone

i'v a jsonObject, which contains the data & i want to display this data in my windows phone screen. this data actually i m retrieving from web service and converting it into the json object. but because of i m new in windows apps, so i'v no idea about grid, canvas etc, so can anyone help me to do this ?

i'v written below code to do this, but all the textblocks are overwritting below one, it might possible that its not correct code--->

for (var rows = 0; rows < jsonObject["data"].Count(); rows++)
            {
                for (var cols = 0; cols < jsonObject["data"][rows].Count(); cols++)
                {
                    ColumnDefinition scheduleTimeColumn = new ColumnDefinition();
                    GridLength timeGrid = new GridLength(10);
                    scheduleTimeColumn.Width = timeGrid;
                    grid1.ColumnDefinitions.Add(scheduleTimeColumn);

                    TextBlock timeTxtBlock = new TextBlock();
                    timeTxtBlock.Text = (String)jsonObject["data"][rows][cols];

                    timeTxtBlock.FontSize = 28;
                    timeTxtBlock.Margin = new Thickness(0, 20, 0, 0);

                    Grid.SetColumn(timeTxtBlock, 0);

                    grid1.Children.Add(timeTxtBlock);
                }
            }

Upvotes: 1

Views: 1517

Answers (1)

PandaSharp
PandaSharp

Reputation: 642

you should create a class with fields that you need

public class C{
public string name {get; set; }
}

an observablecollection of items of the class

ObservableCollection<C> AllAlbums = new ObservableCollection<C>();

create a binding (for example into a listbox)

<ListBox x:Name="albumsListBox" ItemsSource="{Binding}">

                    <ListBox.ItemTemplate>
                        <DataTemplate>

                                        <TextBlock Text="{Binding Name}" />

                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

and bind the collection to listbox

albumsListBox.DataContext = AllAlbums;

Upvotes: 1

Related Questions