Search code examples
c#windows-phone-7pivotviewer

How to programmatically create PivotItems to add to a PivotView?


I am creating an RSS reader and would like to create PivotItems for RSS the user has.

I know that I'll be passing in the number of items into the PivotView so I can create the number of items upon creation.

Does anyone know how I would go about this programmatically?


Solution

  • The best way would be to use the MVVM pattern.

    You could create a view model class for the subscriptions and add them to an ObservableCollection. You then just need to bind the Pivots ItemsSource property to the collection:

            <controls:Pivot ItemsSource="{Binding Path=Subscriptions}">
                <controls:Pivot.ItemTemplate>
                    <DataTemplate>
                        <controls:PivotItem Header="{Binding Path=DisplayName}">
                            <Listbox ItemsSource="{Binding Path=Items}">
                                ...
                                ...
                            </ListBox>
                        </controls:PivotItem>
                    </DataTemplate>
                </controls:Pivot.ItemTemplate>
            </controls:Pivot>