我正在嘗試呼叫一個方法,該方法根據已選擇的 ListBoxItem 用資料填充表。
// Setting the ListBoxItems
myListBox.ItemsSource = list;
// Calling the method when the ListBox's selection changes
myListBox.SelectionChanged = LbItem_Select;
上面的代碼片段不能作業,因為LbItem_Select事件處理程式沒有將當前選擇的專案作為引數。
這是事件處理程式:
private void LbItem_Select(object sender, RoutedEventArgs e)
{
var lbItem = sender as ListBoxItem;
lbItemContent = lbitem.Content.ToString();
// fill the table according to the value of lbItemContent
}
我怎么能做到這一點?
uj5u.com熱心網友回復:
當您使用事件時,sender 是與事件相關的物件。myListBox.SelectionChanged 是 ListBox 的一個事件。所以 sender 是 ListBox,而不是專案。試試這個:
private void LbItem_Select(object sender, RoutedEventArgs e)
{
var listBox = sender as ListBox;
// Or use myListBox directly if you have the ListBox available here
var item = listBox.SelectedItem;
// Do whatever with the item
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/466479.html
下一篇:卡夫卡:listener.name.internal.ssl.endpoint.identification.algorithmvsssl.endpoint.identification.algori
