我有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];
}
任何想法是錯誤我在做什麼?
這是第二次你點擊列表1還是同一個字母? –
這是我第二次點擊不同的字母。給我的確切的錯誤是:索引超出了數組的範圍。 – yalematta
請使用您正在使用的Windows版本和特定應用類型對此進行標記。謝謝。 –