Search code examples
c#xamlwindows-phone-7windows-phone-7.1

Binding code data to XAML pivot listbox


I´m struggling with data binding here for Windows Phone 7.1. I've got a DataLoader class with an OnservableCollection of ItemList (custom class) as an attribute. Therefore, each pivot item has its own list of items. In this DataLoader, I load the data from a JSON a JSON. So far, so good.

public ObservableCollection<ItemList> PivotItem { get; set; }

Here, I store five ItemList, one for each pivot header with his corresponding list of items.

But my problem is that I want to bind this data to the XAML with a ListBox within each PivotItem.

<phone:Pivot Title="iMetrópolis" Loaded="Pivot_Loaded">
        <!--Elemento Pivot 1-->
        <phone:PivotItem 
            x:Uid="PivotItem1"
            Header="Todo" Margin="14,10,10,18">

            <ListBox x:Name="FirstListBox" ItemsSource="{Binding Items}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17" Width="432" Height="78">
                        // I want to add textboxes binding my data
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </phone:PivotItem>
        .
        .
        .

Thanks for the replies!!


Solution

  • Here is what i think you need to do , an example for databinding

            <ListBox x:Name="listBox1">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Height="auto" >
                            <TextBlock Text="{Binding PON}"  />
                            <TextBlock Text="{Binding PIN}" />
    
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
    

    and a class for it

       public class ListObject
        {
            public string PON { get; set; }
            public string PIN { get; set; }
        }
    

    and binding real data from json

         dynamic json = JsonConvert.DeserializeObject(jsondata);
    
         var questions = new List<ListObject>();
          foreach (var abc in json["jsonarray"])
                    {
         var listOfItems = new ListObject();
    listOfItems.PON= abc.object1;
    listOfItems.PIN= abc.object2;
     questions.Add(listOfQuestions);
    
    }
    
      listBox1.ItemsSource = questions;
    

    i hope it helps reply for any comments