我希望你能够帮忙翻译以下文本。这段内容与编程有关,涉及到一个名为 MainListBox 的 XAML 页面中的 ListBox。我可以获取所选项目的索引,但如何获取所选项目的数据?
我的 MainListBox_SelectionChanged 代码:
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int noteID1 = MainListBox.SelectedIndex+1;
if (MainListBox.SelectedIndex != null)
{
//I can get the index that get selected,
Debug.WriteLine(MainListBox.SelectedIndex);
}
MainListBox.SelectedIndex = -1;
}
我的 XAML:
<ListBox x:Name="MainListBox" Margin="6,0,0,0" ItemsSource="{Binding Items}" SelectionChanged="MainListBox_SelectionChanged" Height="578" VerticalAlignment="Bottom" Grid.ColumnSpan="3">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432">
<TextBlock x:Name="ItemText" Text="{Binding noteName}" Margin="-2,-13,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock x:Name="DetailsText" Text="{Binding noteText}" Margin="10,-6,0,3" Style="{StaticResource PhoneTextSubtleStyle}"/>
<TextBlock x:Name="noteIdText" Text="{Binding noteID}" Margin="10,-6,0,3" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
请有人指导我,谢谢。:)
MainListBox.SelectedIndex
计算出笔记ID后检查其是否为空是毫无意义的。在您的检查执行之前,将抛出NullReferenceException
异常。 - Praetorian