我如何访问我选择的项目?列表框
我的 xaml 页面上有一个名为 MainListBox 的 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>
请有人指导我,谢谢。 :)
i have ListBox on my xaml page called MainListBox. i can get index that get selected, but how can i get the data from selected item ?
My 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;
}
my 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>
Please someone guide me, thanks. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设您将
ListBox
的ItemsSource
属性绑定到的Items
对象是MyDataObject 类的对象集合
。然后,在选择更改回调中使用以下内容:Let's say the
Items
object that you're binding theListBox
'sItemsSource
property to is a collection of objects of the classMyDataObject
. Then, within the selection changed callback use the following:感谢大家的快速回复。
终于我明白了。
Thanks everyone for fast reply.
finally i figure it out.
SelectedItem 是 Items 中的实体。可以直接转换为实体类型。
另外,SelectedItem 必须位于 WP7 中的 System.Windows.Control.ListBox 中。这是文档:
http://msdn.microsoft.com/en-us /library/system.windows.controls.listbox.aspx
http://msdn.microsoft.com /en-us/library/system.windows.controls.primitives.selector.selecteditem.aspx
SelectedItem is the entity in the Items. You can directly convert to the entity type.
Also SelectedItem must be in System.Windows.Control.ListBox in WP7. Here is the documentation:
http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.aspx
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selecteditem.aspx
您尝试过 MainListBox.SelectedItem 吗?
var data = MainListBox.Selecteditem as [绑定到列表框的类类型] ;
have you tried MainListBox.SelectedItem ?
var data = MainListBox.Selecteditem as [type of class bounded to listbox] ;