Set SelectedIndex of ListView in FlipView_SelectionChanged event

时光怂恿深爱的人放手 提交于 2019-12-08 07:24:57

问题


I'm building a Photo app using a FlipView. In the BottomAppBar, I placed a ListView of all the images to be able to view the image in the FlipView when I click it in the ListView and get the image displayed in the FlipView, selected in the ListView (like a Pagination).

In the listView.selectionChanged event, I made the code that shows the image in the FlipView when I select it in the ListView. Here is the code:

    private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string CurrentViewState = ApplicationView.GetForCurrentView().Orientation.ToString();
            int IndicatorIndex = listView.SelectedIndex;

            GoToPage(CurrentViewState, IndicatorIndex);
        }

    private void GoToPage(string CurrentViewState, int IndicatorIndex)
        {
            if (CurrentViewState == "Portrait") 
            {
                flipView1.SelectedIndex = IndicatorIndex;
            }
            else if (CurrentViewState == "Landscape")
            {
                if (IndicatorIndex % 2 == 0)
                    flipView1.SelectedIndex = IndicatorIndex / 2;
                else
                {
                    if (IndicatorIndex == 1)
                        flipView1.SelectedIndex = 1;
                    else
                        flipView1.SelectedIndex = (IndicatorIndex + 1) / 2;
                }
            }
        } 

Now when I need to change the listView.SelectedIndex according to the flipView.SelectedIndex

listView.SelectedIndex = flipView.SelectedIndex

I am having an Exception:

An exception of type 'System.ArgumentException' occurred in eBookApp.exe but was not handled in user code. Additional information: Value does not fall within the expected range.

I need to be able to get the same image selected in the FlipView, selected and scrollAt it in the ListView...


回答1:


I ended up making it work by adding to my FlipView:

SelectedIndex="{Binding Path=SelectedIndex, ElementName=listView1, Mode=TwoWay}"

and to my ListView:

SelectedIndex="{Binding Path=SelectedIndex, ElementName=flipView1, Mode=TwoWay}"

Their both SelectedIndex refer to each other!




回答2:


Instead of Selection Change event, a simpler way is using data binding to SelectedIndex.

<Page
    x:Class="BlankApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:BlankApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel Margin="100, 100, 0, 0">
            <FlipView x:Name="flipView1" Width="500" Height="200" SelectionChanged="flipView1_SelectionChanged">
                <Image Source="Assets/Logo.scale-100.png" />
                <Image Source="Assets/SmallLogo.scale-100.png" />
                <Image Source="Assets/SplashScreen.scale-100.png" />
            </FlipView>
            <ListView Name="listview1" SelectedIndex="{Binding Path=SelectedIndex, ElementName=flipView1, Mode=TwoWay}"></ListView>
            <TextBlock Text="{Binding Path=SelectedIndex, ElementName=flipView1}" />
        </StackPanel>
    </Grid>
</Page>


来源:https://stackoverflow.com/questions/29758180/set-selectedindex-of-listview-in-flipview-selectionchanged-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!