2011-04-19 30 views
2

我有我的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> 

请人指导我,谢谢。 :)

+2

检查是否MainListBox.SelectedIndex''是计算音符ID从该值是没有意义的后空。在执行检查之前,将抛出一个'NullReferenceException'。 – Praetorian 2011-04-19 14:56:41

回答

5

你试过MainListBox.SelectedItem吗?

var data = MainListBox.Selecteditem as [类的类型绑定到列表框];

+0

'System.Windows.Controls.ListBox'不包含'Selecteditem'的定义,:(:(似乎不工作的伴侣。任何想法?btw,谢谢 – damniatx 2011-04-19 15:02:28

+0

啊,谢谢你,我需要大写大写的i的SelectedItem。 – damniatx 2011-04-19 16:08:31

0

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

+1

另外,我不认为你的MainListBox.SelectedIndex!= null是正确的。 SelectedIndex是始终不为空的整数。 – Howard 2011-04-19 14:55:44

+0

MainListBox.SelectedItem!= null应该是正确的。 – Howard 2011-04-19 14:56:21

1

比方说,Items对象,你的ListBoxItemsSource属性绑定到是类MyDataObject的对象的集合。然后,在选择更改回调内使用以下内容:

MyDataObject obj = ((sender as FrameworkElement).DataContext) as MyDataObject; 
int noteID = obj.noteID; 
+0

什么是MyDataObject其实呢,如果我没有使用我的数据对象,请使用var。使用此方法var data =((sender as FrameworkElement).DataContext);的Debug.WriteLine(数据); ,输出是wp7_App.MainNotes,看起来像是在正确的方向。任何想法获得noteID ?,谢谢 – damniatx 2011-04-19 15:32:52

+0

MyDataObject是包含字段noteName,noteText和noteID的类。在你的情况下,它看起来像类名是MainNotes。你可以使用var关键字而不是类名。 – Praetorian 2011-04-19 17:37:11

1

感谢大家的快速回复。

终于我知道了。

if (MainListBox.SelectedItem != null) 
     { 


      var data = MainListBox.SelectedItem as Notes; 

      NavigationService.Navigate(new Uri("/DetailsPage.xaml?noteID=" + data.noteID, UriKind.Relative)); 


     } 
0
private void MyList_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    DataModel data = (sender as ListBox).SelectedItem as DataModel; 
    // data.MyPropertyHere; 
} 
相关问题