2013-07-08 23 views
1

我有一個綁定到ListBox源的ObservableCollection。 ListBox使用Style在ListBox的末尾顯示一個按鈕,以「加載更多」內容。當加載最後一個內容時,我更改了ListBox的樣式,以便它顯示「No more posts」。更改列表框樣式而不打頂

問題是ListBox在更改樣式時會轉到頂端(第一項)。有什麼辦法來防止這種情況?

// iterate over items 
foreach (Record rec in root.data.records) 
{ 
    // add items to observablecollection 
} 

// check if there are more items 
if (root.data.nextPage == 0) 
{ 
    // there are no more items => set style with "no more posts" 
    Dispatcher.BeginInvoke(() => listbox.Style = (Style)App.Current.Resources["listboxend"]); 
    return; 
} 
else 
{ 
    // there are still more items => set style with button 
    Dispatcher.BeginInvoke(() => listbox.Style = (Style)this.Resources["listboxwithbutton"]); 
} 

奇怪的事情:在開始時,列表框沒有樣式。當我設置「listboxwithbutton」樣式時,列表框不會跳到頂部......只有在設置「listboxend」樣式時纔會顯示。

回答

0

無論何時更改樣式,您都可以使用ListBox.ScrollIntoView(listBoxItem)滾動到listBoxItem。假設現在你知道如何獲得這個。祝你好運。

+0

我不能使用它,因爲我在「加載更多」功能中進行了更改,並且看到當時添加的下一個項目是最後一個。所以當我改變風格時,用戶並沒有滾動到底部。我希望你明白。 –

+0

你可以添加你的代碼嗎?噸會幫助更多。 –

+0

完成:)查看更新後的問題 –