2014-09-04 14 views
1

當我點擊listBox項目時,我在「selectionChanged」事件中得到一個SubItem。我還需要獲得Title。我如何實現它?如何獲取頁眉信息形成ListBox?

public class Data 
{ 
    public string Title { get; set; } 
    public List<SubItem> SubItems { get; set; } 

    public Data() 
    { 
     SubItems = new List<SubItem>(); 
    }  
} 

<phone:LongListSelector ItemsSource="{Binding DataCollection}" Grid.Row="0"> 
    <phone:LongListSelector.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="{Binding Title}" Padding="5" /> 
        <TextBlock Text="{Binding ImageSource}" Padding="5"/> 
       </StackPanel> 
       <ListBox ItemsSource="{Binding SubItems}" SelectionChanged="ListBox_SelectionChanged"> 
        <ListBox.ItemsPanel> 
         <ItemsPanelTemplate> 
          <StackPanel Orientation="Vertical"/> 
         </ItemsPanelTemplate> 
        </ListBox.ItemsPanel> 
        <ListBox.ItemTemplate> 
         <DataTemplate>         
          <TextBlock Text="{Binding SubItemTitle}" Margin="0,0,12,0" Padding="10" /> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </StackPanel> 
     </DataTemplate> 
    </phone:LongListSelector.ItemTemplate> 
</phone:LongListSelector> 

回答

0

SelectionChanged事件中,您可以通過投射sender參數來檢索您的ListBox。從那裏,您可以通過投射datacontext來檢索您的數據對象:

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    var listBox = (ListBox)sender; 

    var data = (Data)listBox.DataContext; 

    System.Diagnostics.Debug.WriteLine(data.Title); 
}