我有我的xaml页面上名为MainListBox ListBox。我可以得到选择的索引,但我如何从选定的项目获取数据?我如何访问我选择的项目? 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 2011-04-19 14:56:41