2017-09-06 21 views
-1

我在C#中的WPF頁面的代碼listbox.selected錯誤

listBox1.Items[i].Selected = !listBox1.Items[i].Selected; 

和我得到這個錯誤:

Error CS1061 'object' does not contain a definition for 'Selected' and no extension method 'Selected' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) samacrm D:\new\samacrm\samacrm\message.xaml.cs 53 Active

還我測試listBox1.SetSelected(我,真);但我有錯誤 我該怎麼辦?

+4

'listBox1.Items [i]'沒有它的屬性'Selected' ... – ediblecode

+0

似乎這條線在循環中,listBox中的item對象通常是一個字符串,所以它沒有選定的屬性。發佈你的循環代碼,所以我們可以理解你真正想要做什麼 – Youssef13

+0

根據你編輯的問題,你會得到什麼錯誤'listBox1.SetSelected(i,true);'? – Youssef13

回答

3

Listbox沒有'Selected'屬性。您需要使用「的SelectedItem」

查看MSDN瞭解更多信息:Listbox.SelectedItem

1

一個ListBox包含Items屬性,它是一個集由指定的ItemSource。

你需要ListBox.SeleectedItem,如果它是強類型,你將需要退回到你的類型。