I have a Listbox that contains Images as items. we can select the image and it will show the image in image viewer . Now the issue is when ever I load it the scrollViewer is moving to previously selected item in Listbox instead of first item. what i need to do to make scrollViewer of Listbox should be on top every time I load?
Asked
Active
Viewed 358 times
1 Answers
0
You can always use ListBox.ScrollIntoView
to scroll a specific item into view:
if (listBox.Items.Count > 0)
{
object firstItem = listBox.Items[0];
listBox.ScrollIntoView(firstItem);
}

BionicCode
- 1
- 4
- 28
- 44
-
How can we access the listbox data in viewmodel because we can only use them xaml.cs file and I need to handle from Viewmodel – naveenkumar Aug 31 '21 at 12:27
-
No, this is never the resposibility of the view model. The view model is only allowed to select the data models, but it has no knowlegde of a scroll viewer or visual presentation of the data models in general. Your original question is very broad and misses key details, that's why I can only give you this broad answer - broad questions can only get broad answers. You basically have to find a view event and handle it (and execute the above code). What ever event is suitable for you (e.g. `ListBox.SelectionChanged` or `Image.Loaded` - I can't tell) – BionicCode Aug 31 '21 at 12:57