0
votes

Using MVVM Light I have a listbox databound to a collection of several items. One of the items is the selected one.

With this code it works perfectly and ViewModel structures are updated correctly:

 <Name="listBox1"
 ItemsSource="{Binding Path=Models}"
 SelectedItem="{Binding Path=csProfile.Model, Mode=TwoWay}">

My problem is that when I enter the page if the selected Item is not in the first items it is not visible and the user does not know what was the previous selection.

How could I force the Listbox to always show the Selected Item?

Possibly via properties or Binding.

M

2

2 Answers

0
votes

There's no property you can bind to to set what's visible. Instead call ScrollToVerticalOffset() on the ScrollViewer inside the ListBox.

0
votes

I think you are looking for the ScrollIntoView method. A similar topic was discussed here: Automatic Scrolling in a Silverlight List Box

The API reference is here: http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.scrollintoview(VS.95).aspx

Calling UpdateLayout() before calling ScrollIntoView on the selected item seems to be necessary.