[英]How to select list box items that are in a list?
我有一個列表框,其中包含各種卡車功能。 featureListBox.ItemsSource = DAO.GetAllFeatures()
我還有一個 TruckWithModel 類,它有一個屬性,其中包含特定卡車具有的功能列表。 TruckWithModel m = DAO.SearchByRego(regoComboBox.SelectedItem.ToString());
使用以下代碼,我試圖選擇列表框中的功能。
for(int i = m.Features.Count -1; i>=0; --i)
{
featureListBox.SelectedItems = m.Features[i];
}
但是,由於此屬性是只讀的 (.selecteditems),因此沒有選擇任何屬性。
如何在列表框中選擇與列表中的功能匹配的功能?
我有點猜測,但假設m.Features[i]
是一個字符串,你可以做這樣的事情:
for (int i = m.Features.Count -1; i >= 0; --i)
{
var index = featureListBox.FindString(m.Features[i]);
featureListBox.SetSelected(index, true);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.