2015-11-05 84 views
5

我有2個ListViews和一個TextBlock。第一個ListView1包含按字母順序排列的字母。第二個ListView2包含以選定字母開頭的單詞(在ListView1中)。當我從ListView1中選擇一個字母,然後單擊一個在ListView2中加載的單詞時,我想要在TextBlock中獲得該單詞的定義。無法在Xaml中將SelectedIndex =「0」設置爲ListView(Windows應用商店應用)

這是我的XAML:

<ListView 
       Width="510" 
       x:Name="ListView1" 
       ItemsSource="{Binding}" 
       Background="White" 
       Foreground="Black" 
       TabIndex="1" 
       Margin="-7,8,0,0" 
       IsSwipeEnabled="False" 

       SelectionChanged="ItemListView_SelectionChanged" 
       Grid.Row="1" 
       HorizontalAlignment="Left"> 
       <ListView.ItemTemplate> 
        <DataTemplate> 
         <StackPanel> 
          <TextBlock Grid.Row="0" 
          Text="{Binding glossary_letter}" 
      Margin="10,0,0,0" 
      TextWrapping="Wrap" 
          Foreground="Black" 
      FontSize="24" 
      FontWeight="SemiBold" 
      VerticalAlignment="Center"/> 
         </StackPanel> 
        </DataTemplate> 
       </ListView.ItemTemplate> 
      </ListView> 
      <ListView Width="361" 
       x:Name="ListView2" 
       Background="White" 
       Foreground="Black" 
       Margin="425,8,230,0" 
       Grid.Row="1" 
       HorizontalAlignment="Center" 
       ItemsSource="{Binding}" 
       SelectionChanged="itemListView2_SelectionChanged"> 
       <ListView.ItemTemplate> 
        <DataTemplate> 
         <StackPanel> 
          <TextBlock Grid.Row="1" 
      TextWrapping="Wrap" 
          Foreground="Black" 
          Text="{Binding}"  
      FontSize="24" 
      FontWeight="SemiBold" 
      VerticalAlignment="Center"/> 
         </StackPanel> 
        </DataTemplate> 
       </ListView.ItemTemplate> 
      </ListView> 
      <StackPanel HorizontalAlignment="Right" 
         Background="White" 
         Width="580" 
         Margin="0,10,0,0" Grid.Row="1" Grid.ColumnSpan="2"> 
       <TextBlock x:Name="defBlock" Foreground="Black" Text="{Binding glossary_definition}"></TextBlock> 
      </StackPanel> 

如果我點擊第一次上的一個字母(ListView1的),然後就一個字(ListView2)它讓我的定義。但是我第二次點擊一個字母,它給了我一個OutOfRange錯誤的ListView2.SelectedIndex = -1

這是我的C#代碼:

private void ListView1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     {   
      ListView2.ItemsSource = arrayW[ListView1.SelectedIndex];   
     } 
    private void ListView2_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
    defBlock.Text = arrayDef[ListView1.SelectedIndex][ListView2.SelectedIndex];  
    } 

任何想法是錯誤我在做什麼?

+0

這是第二次你點擊列表1還是同一個字母? –

+0

這是我第二次點擊不同的字母。給我的確切的錯誤是:索引超出了數組的範圍。 – yalematta

+0

請使用您正在使用的Windows版本和特定應用類型對此進行標記。謝謝。 –

回答

2
private void ListView2_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 

    if(ListView2.SelectedIndex >= 0){ 
     defBlock.Text = arrayDef[ListView1.SelectedIndex][ListView2.SelectedIndex];  
    } 
    else 
    { 
     defBlock.Text = arrayDef[ListView1.SelectedIndex][0];//set default selected word.. 
    } 
} 
+0

很好的答案,謝謝你。 –

0

問題
你需要管理你的列表2選擇的指數變化的處理程序,因爲每一次你更新你的列表中的一個存在於列表2選擇的指數變化,因爲沒有選擇指數則默認爲-1 。

有很多方法可以做到這一點。

private void ListView2_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
    if(ListView2.SelectedIndex == -1) 
    // do something or 
    // eg. 
    return; 
    // or 
    throw new IndexOutOfRangeException("Message"); 
    //or 
    throw new Exception(); // catch all    
    } 

2.

我不知道你怎麼想您的應用程序看起來像。

我會爲此使用兩個單獨的頁面。將xaml用於第一個列表視圖,然後將第二個頁面查看並綁定到第一個頁面的選定索引。

因此,您選擇list1,然後在顯示list2的新頁面中更容易設置爲數據源,然後您可以使用選定項目的詳細信息更新您的文本框。或者更進一步,如果你想展示更廣泛的單詞細節及其定義,請創建第三頁。

這樣,當數據源發生更改時,您不會遇到List2沒有選定索引的問題。


或者, 就拿綁定聲明瞭該指數的變化處理程序,有條不紊地給他們打電話時的指數是在列表1是selected.So當列表1的選擇發生變化時,清單2中更新換句話說,你需要更新你的數據源。 編輯:使用這種方法,您可以控制錯誤處理的使用,以避免超出範圍的異常,因爲數據源已更新。

所以可能把以下內容放到一個單獨的方法中。

private void MyTextMethod(){ 

    defBlock.Text = arrayDef[ListView1.SelectedIndex][ListView2.SelectedIndex]; 
} 

private void ListView2_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
    try{ 

     MyTextMethod)(); 
    } 
    catch(OutOfRangeException){ 
     // do something. 
    } 
} 

從您選擇的索引中更改處理程序,並從處理程序中調用單獨的方法。

4。

從list1的selectedindex更改處理程序中取出list2的綁定聲明。

所以你可以有一個方法來更新list2的綁定源並管理選定的索引改變處理程序。雖然這是最沒有用的建議。

底線:您需要有一些嘗試和捕捉,或拋出聲明管理超出範圍的異常,因爲第二個列表將具有不同的長度,並且字母As列表中的索引可以選擇10,然後字母X可能只有一個長度爲1的列表,並且始終存在selectionchange返回選擇-1的問題。

(你實際上並不需要清除列表2,作爲數據源改變時它會自動清零(對不起,我沒有說清楚))

+0

我創建了一個方法: 'public void FillWords() { ListView2.Items.Clear(); ListView2.ItemsSource = arrayW [ListView1.SelectedIndex]; }' 我從ListView1的SelectionChanged事件處理程序調用它。但是我在'ListView2.Items.Clear();'上得到了一個「災難性失敗」錯誤;' – yalematta

+0

因爲我有SelectionMode = SingleMode,所以我不能使用'ListView2.Items.Clear();'我使用了'ListView2。 SelectedItem = null;'但我仍然得到第一個錯誤'OutOfRange'錯誤'ListView2.SelectedIndex = -1' – yalematta

+0

對不起,我想我的第一個答案不清楚。這是我的錯。我希望這會爲你清除它。 :) –

相關問題