SelectedItem set to the first item with CollectionViewSource

I have a view data binding via mvvm light to viewmodel in my WP7 project. The view contains a list with the following settings:

<ListBox x:Name="StationList" ItemsSource="{Binding StationList}" SelectedItem="{Binding SelectedStation, Mode=TwoWay}" > 

The StationList is an ObservableCollection.

Now that the view is loading, everything looks great! A list is displayed and NO is selected.

But when I change XAML to:

 <ListBox x:Name="StationList" ItemsSource="{Binding Source={StaticResource StationListSorted}}" SelectedItem="{Binding SelectedStation, Mode=TwoWay}" > 

When StationListSorted is a simple one sorting property in StationList as a CollectionViewSource. Now everything is getting ugly !! In the same view, the same elements in the list are loaded, but now they are sorted correctly, BUT the first element is selected and the selectedItem property is set!

How can I sort a ListBox with a CollectionViewSource WITHOUT automatically selecting my first item?

+6
source share
1 answer

In the list, select the IsSynchronizedWithCurrentItem parameter and see which value (true or false) causes the desired effect.

+15
source

Source: https://habr.com/ru/post/893958/


All Articles