2011-04-19 126 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; 
} 
相關問題