Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the SelectedIndex proprety of Pivot in UWP

Tags:

c#

xaml

uwp

I have a Page with only a pivot in it. This page is always cached. Now whenever I navigate to this page, I want its contents and selections to be loaded from cached but I want to have the first PivotItem in view. XAML :

<Pivot x:Name="FilterPivot" 
        IsHeaderItemsCarouselEnabled="True"
        SelectedIndex="0">

    <PivotItem Header="Author" >               

        <ListBox ItemsSource="{x:Bind AuthorFacets, Mode=OneWay}"
                    Name="AuthorListBox"                         
                    SelectionMode="Multiple"
                    SelectionChanged="AuthorListBox_SelectionChanged">

            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                </Style>
            </ListBox.ItemContainerStyle>


            <ListBox.ItemTemplate>
                <DataTemplate x:DataType="local:IFacet">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="0"
                                        Text="{x:Bind read}"
                                        TextWrapping="Wrap"
                                        HorizontalAlignment="Left"
                                        VerticalAlignment="Center"/>

                        <Border Grid.Column="1"
                                    Background="Gray"
                                    MinWidth="25"
                                    CornerRadius="8">
                            <TextBlock Text="{x:Bind num}"
                                            HorizontalAlignment="Center"
                                            VerticalAlignment="Center"
                                            Padding="2"/>
                        </Border>

                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </PivotItem>


    <PivotItem Header="Language">

        ....
        ....

    </PivotItem>

    <PivotItem Header="Learning Resource Type">

        ....
        ....

    </PivotItem>



    <PivotItem Header="Subject">

        ....
        ....

    </PivotItem>

    <PivotItem Header="Type">

        ....
        ....

    </PivotItem>

    <PivotItem Header="Education Level">

        ....
        ....

    </PivotItem>

    <PivotItem Header="Source">

        ....
        ....

    </PivotItem>

</Pivot>

This is my code behind:

public FilterPage()
{
    this.InitializeComponent();
    this.NavigationCacheMode = NavigationCacheMode.Required;
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    FilterPivot.SelectedIndex = 0;
}

This doesn't select the 0th index instead, it shows the cached PivotItem that was selected when the user navigated away from the view.

like image 659
ravi kumar Avatar asked Jun 14 '17 06:06

ravi kumar


1 Answers

The OnNavigatedTo will be invoked before the Pivot control is even loaded, so setting the SelectedIndex won't affect its selection.

The easy workaround is to subscribe to the Loaded event instead and set it from there.

Just do the following within your MainPage's constructor -

FilterPivot.Loaded += (s, e) => FilterPivot.SelectedIndex = 0;
like image 130
Justin XL Avatar answered Sep 23 '22 04:09

Justin XL