Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ListBox SelectedItem sometimes is not visible (scrolling is out of sync)

Tags:

wpf

listbox

I have a Listbox that is filled with 30,000 elements

<ListBox Name="lbWordlist" 
             Grid.Row="1" Margin="10"
             ItemsSource="{Binding Source={StaticResource WordListViewSource}}" 
             SelectedItem="{Binding Source={StaticResource MainViewModel}, Path=SelectedArticle}"
             IsSynchronizedWithCurrentItem="True"
             SelectionChanged="lbWordlist_SelectionChanged" />

I'm tracking history of user clicking items

I have 2 buttons Prev. and Next, these buttons allow user to go back and forward in history.

Prev. and Next button modify SelectedItem property of ListBox

The problem comes up, if user selects random items from ListBox that are very apart from each other (for example user selected some items on top of the list and few items in the bottom of the list).

Prev. and Next button change SelectedItem property, but ListBox is not synced with scroll, selected item is not highlighted and most of the times is not visible to user, as it's outside of ListBox's visible items area.

Is there any way to make ListBox correctly show currently selected item?

Thank You.

like image 357
Daniil Harik Avatar asked Mar 31 '09 08:03

Daniil Harik


1 Answers

_listBox.ScrollIntoView(_listBox.SelectedItem);
like image 60
Kent Boogaart Avatar answered Nov 02 '22 06:11

Kent Boogaart