有人知道如何從運行時生成的列表框項中提取文本塊文本嗎?我有一個自定義的 ListBoxItem,其中包含一個 SymbolIcon 和一個 textBlock。我只需要 textBlock 值。private void AlbumSongList_SelectionChanged(object sender, SelectionChangedEventArgs e) { var selected = AlbumSongList.SelectedItem as ListBoxItem; DataTemplate template = selected.ContentTemplate; Debug.WriteLine("You have selected the song: " + selected.ToString()); }這是包含文本塊的自定義控件同樣,如果您看到具有 Sing 文本綁定源的“Textblock”,這就是我需要的文本值。
1 回答

慕雪6442864
TA貢獻1812條經(jīng)驗 獲得超5個贊
ListBox具有默認(rèn)選擇行為,該行為取決于項目源是什么(用于 的類型ItemsSource)。所以 theSelectedItem是你的Model類的類型,所以你可以TextBlock從你選擇的模型中獲取 的文本。
例如,您的模型類 Name 是Model,那么您可以使用以下代碼獲取文本,
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selected = AlbumSongList.SelectedItem as Model;
Debug.WriteLine("You have selected the song: Song Number is {0}, and Song is {1}",
selected.SongNumber, selected.Song);
}
- 1 回答
- 0 關(guān)注
- 309 瀏覽
添加回答
舉報
0/150
提交
取消