I typically set IsSynchronizedWithCurrentItem="True"
on the ListBox
. Then I add a SelectionChanged
handler and always bring the selected item into view, with code like this:
private void BringSelectionIntoView(object sender, SelectionChangedEventArgs e)
{
Selector selector = sender as Selector;
if (selector is ListBox)
{
(selector as ListBox).ScrollIntoView(selector.SelectedItem);
}
}
From my VM I can get the default collection view and use one of the MoveCurrent*()
methods to ensure that the item being edited is the current item.
CollectionViewSource.GetDefaultView(_myCollection).MoveCurrentTo(thisItem);
NOTE: Edited to use ListBox.ScrollIntoView()
to accomodate virtualization
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…