SelectedItem set to first item with CollectionViewSource

試著忘記壹切 提交于 2019-12-18 05:03:57

问题


I have a view databound through mvvm light to a viewmodel in my WP7 project. The view contains a Listbox with following settings:

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

The StationList is a ObservableCollection.

Now when the view gets loaded, everything looks great! The list is shown and NO item is selected!

But when I change the XAML to:

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

With the StationListSorted being a simple one property sort on the StationList as a CollectionViewSource. Now things turn ugly!! The same view is loaded with the same items in the listbox, but now correctly sorted, BUT the first item is selected and the selectedItem property is set!!

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


回答1:


On your listbox, try setting IsSynchronizedWithCurrentItem and see which value (either true or false) produces the desired effect.



来源:https://stackoverflow.com/questions/6885837/selecteditem-set-to-first-item-with-collectionviewsource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!