2015-04-20 81 views
1

我正在使用FlipView構建一個Photo應用程序。在BottomAppBar中,我放置了所有圖像的ListView以便能夠在FlipView中查看圖像,當我在ListView中單擊它時,圖像顯示在ListView(如分頁)中選擇的FlipView中。在FlipView_SelectionChanged事件中設置ListView的selectedIndex

listView.selectionChanged事件中,當我在ListView中選擇它時,我製作了顯示FlipView中圖像的代碼。下面是代碼:

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; 
       } 
      } 
     } 

現在,當我需要根據flipView.SelectedIndex

listView.SelectedIndex = flipView.SelectedIndex 

我有一個異常改變listView.SelectedIndex

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. 

我需要能夠獲得在FlipView中選擇的相同圖像,並將其選中並滾動到ListView ...

+0

請比「例外」更具體。你得到什麼異常?什麼是異常的確切錯誤信息?什麼是異常堆棧跟蹤?請提供可靠地重現問題的[良好,_minimal_,_complete_代碼示例](http://stackoverflow.com/help/mcve),以及有關如何使用該代碼示例重現問題的明確具體說明。 –

+0

我試着編輯我的問題,我添加了我收到的異常消息和更多的代碼,希望這是有幫助的! – yalematta

回答

1

我最終作出加入到我的FlipView它的工作:

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

和我ListView

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

他們都SelectedIndex稱對方!

0

而不是選擇更改事件,更簡單的方法是使用數據綁定到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> 
+0

我嘗試在Xaml的ListView中添加'SelectedIndex =「{Binding Path = SelectedIndex,ElementName = flipView1,Mode = TwoWay}」'但它**不起作用!** – yalematta

+0

您是否已將ElementName更改爲您的FlipView名稱?在我的情況下,它是flipView1。 –

+0

是的,我做了,我刪除了我的ListView OnSelectionChanged事件,仍然當我更改FlipView選擇,沒有項目被選中我的ListView – yalematta

相關問題