2012-02-28 164 views

回答

1

後右(或之後的任何點)設置的datacontext的列表框(或父對象 - 可能是頁面),只需將選定的索引設置爲列表中的第一項。

listbox.SelectedIndex = 0; 

如果你有一個處理器,當選定的索引被改變時,一定要忽略當你第一次設置索引。

0

你爲什麼不嘗試像

var listBoxItem = ItemContainerGenerator.ContainerFromItem(myList.First()); 
listBoxItem.Focus(); 

listBoxItem.IsSelected = true; 
+0

我應該什麼時候這樣做?我必須使用特定事件來確保所有數據都已加載? – Sergejs 2012-02-28 16:45:32

+0

是的,只要你的數據有界,並且你確定ListBox有一些項目,你就可以這麼做...... otherway'listBoxItem'將是空的 – GaaRa 2012-02-28 16:52:13

1

創建一個名爲IsSelected中包含的ObservableCollection內的對象屬性。將此綁定到ListBoxItemIsSelected屬性通過TwoWay綁定。

然後,在頁面的OnLoaded回調(或任何你要綁定的集合到ListBox),這樣做

foreach(var obj in myCollection) { 
    obj.IsSelected = false; 
} 
if(myCollection.Count > 0) { 
    myCollection[0].IsSelected = true; 
} 
// bind the collection to the listbox 
相關問題