[英]ListBox.SelectedItem refuses to be set
以下代碼中引發異常的可能原因是什么?
var oldItem = this.MyListBox.SelectedItem;
if (this.MyListBox.Items.Contains(newItem))
{
this.MyListBox.SelectedItem = newItem;
if (this.MyListBox.SelectedItem != newItem && this.MyListBox.SelectedItem == oldItem)
throw new ApplicationException("WTF?");
}
絕對不會引發ListBox.SelectionChanged事件。
編輯:oldItem和newItem是相同類型的簡單業務對象。 它們不為空。
您需要使用SetSelected方法,如下所示:
MyListBox.SetSelected(index, true);
或將其設置在商品本身上,如下所示:
MyListBox.Items(index).Selected = true;
我不確定您的問題中有什么newItem
,因此您需要在列表中標識它的索引,並將其放在上面的代碼片段中我有index
的位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.