Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Databinding PivotItems to ObservableCollection on WP7

I want to databind an ObservableCollection to a Pivot contronl in WP7 so that each object in my ObservableCollection becomes a PivotItem. This is the code I use:

    <controls:Pivot x:Name="MainPivot" ItemsSource="{Binding Persons}">
        <controls:Pivot.HeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding FullName}"/>
            </DataTemplate>
        </controls:Pivot.HeaderTemplate>
        <controls:Pivot.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="0,0,0,17" Width="432">
                    <TextBlock Text="{Binding FirstName}"/>
                    <TextBlock Text="{Binding LastName}"/>
                    <TextBlock Text="{Binding HomeTown}"/>
                </StackPanel>
            </DataTemplate>
        </controls:Pivot.ItemTemplate>
    </controls:Pivot>

This works and with tre items in my ObservableCollection I get three PivotItems. But when everything gets loaded the binding inside the DataTemplate won´t get updated. It is only when I scroll to the next PivotItem that the FirstName, LastName and HomeTown gets loaded.

Why is that? What am I missing?

Thanks

like image 941
Björn Eriksen Avatar asked Feb 03 '11 09:02

Björn Eriksen


2 Answers

Check this discussion: DataBound Pivot control is not creating the first PivotItem

like image 137
Boryana Miloshevska Avatar answered Sep 30 '22 15:09

Boryana Miloshevska


I had the same problem, but the workaround with setting SelectedIndex=1 didn't suit me.

I found the other solution: when you adding the Item to your Persons collection you should first create a temp element and only when you fill all data add it to your Persons collection.

Person tempPers = new Person() { FullName = "Abduvaliev Edem", FirstName = "Edem", LastName = "Abduvaliev", HomeTown = "Sevastopol"};
Pesrons.Add(tempPers);
like image 20
Edem Abduvaliev Avatar answered Sep 30 '22 14:09

Edem Abduvaliev