【问题标题】:Cannot set SelectedIndex="0" in Xaml to ListView (Windows Store App)无法将 Xaml 中的 SelectedIndex="0" 设置为 ListView(Windows 应用商店应用程序)
【发布时间】:2015-11-05 13:49:45
【问题描述】:

我有 2 个 ListView 和一个 TextBlock。第一个 ListView1 包含按字母顺序排列的字母。第二个 ListView2 包含以所选字母开头的单词(在 ListView1 中)。当我从 ListView1 中选择一个字母,然后单击 ListView2 中加载的一个单词时,我想在 TextBlock 中获取该单词的定义。

这是我的 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];       
   }

知道我在做什么错误吗?

【问题讨论】:

  • 这是我第二次点击不同的字母。给我的确切错误是:索引超出了数组的范围。
  • 这是一个使用 C# 和 Xaml 在 Windows 8.1 上的 Windows Store 应用程序。

标签: c# xaml listview windows-store-apps selectedindex


【解决方案1】:
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.. 
    }
}

【讨论】:

    【解决方案2】:

    问题
    您需要管理您的 list2 选定索引更改处理程序,因为每次更新列表 1 时,列表 2 都会发生选定索引更改,并且由于没有选定索引,它默认为 -1。

    有多种方法可以做到这一点。
    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 在数据源更改时不会出现没有选定索引的问题。

    3.
    要么, 从索引更改处理程序中取出绑定声明,并在选择 List1 中的索引时有条不紊地调用它们。因此,当 List1 的选择发生更改时,List 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 的绑定源并管理选定的索引更改处理程序。虽然这是最没用的建议。

    底线:您需要一些 try and catch 或 throw 语句来管理超出范围的异常,因为第二个列表将具有不同的长度,并且字母 As 列表上的索引可能选择为 10,然后字母 X 可能只有一个长度为 1 的列表,并且总是存在 selectionchange 返回选择 -1 的问题。

    (list2其实不需要清空,数据源变化时会自动清空(抱歉,没说清楚))

    【讨论】:

    • 我创建了一个方法:public void FillWords() { ListView2.Items.Clear(); ListView2.ItemsSource = arrayW[ListView1.SelectedIndex]; } 我从 ListView1 的 SelectionChanged 事件处理程序中调用它。但我在ListView2.Items.Clear(); 上遇到“灾难性故障”错误
    • 因为我有 SelectionMode = SingleMode,我不能使用 ListView2.Items.Clear(); 我使用 ListView2.SelectedItem = null; 但我仍然收到第一个错误 OutOfRange 错误,其中 ListView2.SelectedIndex = -1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 2014-08-13
    相关资源
    最近更新 更多