dleviathan
dleviathan

Reputation: 87

About selected specific item in flipview window phone 8.1

I have program app by winRT(Win phone 8.1) and i use FlipView control as below:

 <FlipView x:Name="flvMain" Margin="0,0,0,0" Background="Transparent" SelectedIndex="1" SelectionChanged="flvMain_SelectionChanged" Tapped="flvMain_Tap">
         <!-- Flip View Item 1 -->
        <Grid x:Name="grdOption" >
            <Grid.RowDefinitions>
                <RowDefinition Height="120"/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0">
                <Grid.Background>
                    <ImageBrush Stretch="Fill" ImageSource="Assets/test1.png"/>
                </Grid.Background>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="50"/>
                </Grid.ColumnDefinitions>
                <Image x:Name="imgAvatar" Grid.Column="0" Margin="10,15" Source="Assets/Logo.png"/>
                <Grid Grid.Column="1" Margin="0,15">
                    <TextBlock x:Name="txbNameAlias" Margin="0,5,0,54" FontSize="22" FontWeight="Bold" SelectionHighlightColor="{x:Null}" Foreground="White" RequestedTheme="Light">Name</TextBlock>
                    <TextBlock x:Name="txbAccountId" Margin="0,36,0,0" FontSize="16" RequestedTheme="Light" Foreground="White" Height="20" VerticalAlignment="Top">55556666</TextBlock>
                    <TextBlock x:Name="txbAmount" Margin="0,61,0,0" FontSize="18" FontWeight="Bold" RequestedTheme="Light" Foreground="#FFFFFEFE">555555</TextBlock>
                </Grid>
                <Button x:Name="btnBackToMain" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinHeight="50" MinWidth="40" Margin="5,30" BorderBrush="Transparent" RenderTransformOrigin="0.54,-0.035" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Click="btnBackToMain_Click">
                    <Button.Background>
                        <ImageBrush Stretch="Fill" ImageSource="Assets/icon-back-mainpage.png"/>
                    </Button.Background>
                </Button>
            </Grid>
            <Grid Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Button Grid.Column="0" BorderThickness="0" RequestedTheme="Light" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#FF0C75C9" Margin="0,-10,0,-10" MinHeight="58" Content="Btn 1" ScrollViewer.VerticalScrollBarVisibility="Disabled" Foreground="#FFFFFEFE"  FontSize="18" FontFamily="Global User Interface" />
                <Button Grid.Column="1" BorderThickness="0" RequestedTheme="Light" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#FF308D1E" Margin="0,-10" MinHeight="58" ScrollViewer.VerticalScrollBarVisibility="Disabled" FontFamily="Global User Interface" FontSize="18" Content="btn 2" Foreground="White"/>
            </Grid>
            <Grid Grid.Row="2">
                <ListView x:Name="lvFunctionOption" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemClick="lvFunction_ItemClick" IsItemClickEnabled="True">
                   <!-- some code here -->
                </ListView>
                <Grid x:Name="grdSubListFunction" >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <ListView Grid.Column="1" x:Name="lvSubFunctions" Background="#00D53636" Margin="0" Visibility="Collapsed" RequestedTheme="Light" IsItemClickEnabled="True" HorizontalAlignment="Stretch" VerticalAlignment="Top" ItemClick="lvSubFunctions_ItemClick">
                       <!-- some code here -->
                    </ListView>
                </Grid>
            </Grid>
        </Grid>
         <!-- Flip View Item 2 -->
        <Grid x:Name="grdMain" Height="640" Margin="0,0,0,0" Background="White">
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0" Background="#FF0C75C9">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50"/>
                </Grid.ColumnDefinitions>
                <Image x:Name="btnToListFunction" Grid.Column="0" Source="Assets/icon-menu.png" Margin="7" Tapped="btnToListFunction_Tap"></Image>

            </Grid>
            <ScrollViewer Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Top">
                <StackPanel Background="White">
                    <!-- some code here -->
                </StackPanel>
            </ScrollViewer>
        </Grid>
    </FlipView>

and in xaml.cs file i have code

  protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        flvMain.SelectedIndex = 1;
    }

it run normal but when i click on btnBackToMain and tap on btnToListFunction i have code:

 private void btnBackToMain_Click(object sender, RoutedEventArgs e)
    {
        flvMain.SelectedIndex = 1;
    }

    private void btnToListFunction_Tap(object sender, TappedRoutedEventArgs e)
    {
        flvMain.SelectedIndex = 0;
    }

flipview can't change item to sepecific index. When I debug click event by SelectionChangedEvent

    private void flvMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (flvMain != null)
        {
            CustomTool.Log(this, "current index: " + flvMain.SelectedIndex);
        }
        else
        {
            CustomTool.Log(this, "NULLL");
        }
    }

I have result as with tap btnToListFunction :

Log: MainPage >> current index: 0
Log: MainPage >> current index: 1

and click btnBackToMain

Log: MainPage >> current index: 1
Log: MainPage >> current index: 0

But when I swipe to right(same click on btnBackToMain) i have result:

Log: MainPage >> current index: 1

and swipe to left (same click on btnToListFunction ) i have result:

Log: MainPage >> current index: 0

Please help me solve it? I don't know why selectedIndex of flipview can be roll back after it change (by click). Thanks for all suport!

Upvotes: 0

Views: 632

Answers (1)

Young-Chung Hsue
Young-Chung Hsue

Reputation: 116

I have a similar problem and the difference is that my button is AppBarButton. If you use mouse to click the button, it will work fine; therefore, I guess the problem is caused by some events launched by manipulation. I solve this problem by declaring

ManipulationMode="None"

for the button inside the FlipView.

So, you can solve this problem by embedding ManipulationMode="None" into your btnBackToMain and btnToListFunction elements in XAML.

Hope this solution is helpful for you.

[2015-08-18] Added

If you don't want to set Manipulation as None to ignore all the manipulation upon your buttons, you can try to set flvMain.SelectedIndex = -1; before setting the flvMain.SelectedIndex as the number you want. For example,

      private void btnBackToMain_Click(object sender, RoutedEventArgs e)
    {
        flvMain.SelectedIndex = -1;
        flvMain.SelectedIndex = 1;
    }

    private void btnToListFunction_Tap(object sender, TappedRoutedEventArgs e)
    {
        flvMain.SelectedIndex = -1;
        flvMain.SelectedIndex = 0;
    }        

since the problem only occurs when the original index succeeds the next one and -1 is available for SelectedIndex for nothing be selected.

Why does FlipView behave like this way? I don't know. Perhaps it can avoid volatile behavior when people lay their finger upon it. ^_^

Upvotes: 1

Related Questions