SMM
SMM

Reputation: 201

How to execute code to display a PivotItem?

I have a Pivot named MyPivot, and a PivotItem named MyPivotItem. I would like to run the code every time I see the MyPivotItem. Is that right?

if (MyPivot.SelectedItem == MyPivotItem)
{
    //...
}

The code, however, did not work. Where am I wrong?

Upvotes: 0

Views: 33

Answers (2)

Łukasz Rejman
Łukasz Rejman

Reputation: 1892

Your code is fine, but you have to hook SelectionChanged event which fires when you swipe pivot pages to execute it.

XAML

<phone:Pivot x:Name="MyPivot" 
             Title="TITLE" 
             SelectionChanged="Pivot_SelectionChanged">
    <phone:PivotItem x:Name="MyPivotItem" 
                     Header="one" />
    <phone:PivotItem x:Name="AnotherPivotItem" 
                     Header="two" />
</phone:Pivot>

C#

private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (MyPivot.SelectedItem == MyPivotItem)
    {
        //
    }
}

Upvotes: 1

Igor Ralic
Igor Ralic

Reputation: 15006

Why not compare the SelectedIndex?

if (MyPivot.SelectedIndex == someIndex)
{

}

where someIndex is index of your MyPivotItem.

Upvotes: 0

Related Questions