Alpine13
Alpine13

Reputation: 152

Binding pivotitem content in WP8.1

In my mainpage.xaml I have a pivot control:

<Pivot x:Name="Pivot" 
  ItemsSource="{Binding PivotItems}" 
  ItemTemplate="{StaticResource PivotItemTemplate}">
</Pivot>

which binds it's items to a PivotsItems property on my viewmodel. The pivotitems are shown correctly but i'm not able to bind the content of the pivotitem.

My datatemplate looks like:

<DataTemplate x:Key="PivotItemTemplate">
    <PivotItem>
        <TextBlock Text="Test"></TextBlock>
    </PivotItem>
</DataTemplate>

But no content is shown in any pivotitem.

What I wan't to do is to bind the pivotitems (which works) but show a seperate filter for each pivotitem. In this example I just want to show a TextBlock in each pivotitem.

SOLVED:

I defined the itemtemplate on the pivot control in the page, while i had to add it when populating the list in the vm:

PivotItems.Add(new PivotItem { Header = item.Key, ContentTemplate = App.Current.Resources["PivotItemTemplate"] as DataTemplate });

Upvotes: 0

Views: 850

Answers (1)

Kai Brummund
Kai Brummund

Reputation: 3568

Pivot automatically Wraps it's items into PivotItems (like you don't add ListViewItem to a ListViews ItemsTemplate). Just put your content into the DataTemplate.

<DataTemplate x:Key="PivotItemTemplate">
    <TextBlock Text="Test"></TextBlock>
</DataTemplate>

The header defaults to the DataContext of the PivotItem (so your list item). If you want it to display a certain property, you have to set the HeaderTemplate too:

<DataTemplate>
    <TextBlock Text="{Binding MyProperty}" />
</DataTemplate>

If you just keep it as a TextBlock with no further properties, the default style gets applied perfectly.

Upvotes: 2

Related Questions