0

內的StackPanel的子元素的信息,我知道應該有一個簡單的解決這個問題,但我只是不能似乎這裏弄明白什麼是我的代碼如下所示:獲取列表框

<ListBox HorizontalAlignment="Left" 
     x:Name="locationsNB" 
     VerticalAlignment="Top" 
     Height="563" 
     Width="455" 
     SelectionChanged="locationsNB_SelectionChanged"> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <StackPanel Orientation="Horizontal" 
        Margin="18,0,0,0" 
        x:Name="placeDetails"> 
     <Image Source="{Binding icon}" 
       Height="40" 
       Width="40" 
       VerticalAlignment="Top" 
       Margin="0,10,8,0" /> 
     <StackPanel Width="350"> 
      <TextBlock Text="{Binding name}" 
        FontSize="35" 
        Foreground="#399B81" 
        TextWrapping="Wrap" /> 
      <TextBlock Text="{Binding vicinity}" 
        FontSize="20" 
        Foreground="#888888" 
        TextWrapping="Wrap" /> 
      <TextBlock x:Name="reference" 
        Text="{Binding reference}" 
        Visibility="Collapsed" /> 
     </StackPanel> 
     </StackPanel> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

我想獲取所選項目的stackpanel->參考文本(Text =「{Binding reference}」)我不知道我的C#應該是什麼樣子,但任何幫助將不勝感激。

回答

0

如果你的ListBox的ItemsSource綁定到項目的集合,那麼你可以使用列表框

private void locationsNB_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    var listbox = (ListBox)sender; 

    var myObject = listbox.SelectedItem as MyCustomObject; 
    if (myObject == null) return; 

    // perform your custom logic with this item 
} 
+0

是啊謝謝你洙很是很容易的SelectedItem屬性:) @Shawn Kendrot – Denzil