问题描述
我有一个通过 mvvm light 绑定到我的 WP7 项目中的视图模型的视图数据.该视图包含一个具有以下设置的列表框:
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}"
>
StationList 是一个 ObservableCollection.
The StationList is a ObservableCollection.
现在当视图被加载时,一切看起来都很棒!显示列表,但未选择任何项目!
Now when the view gets loaded, everything looks great! The list is shown and NO item is selected!
但是当我将 XAML 更改为:
But when I change the XAML to:
<ListBox x:Name="StationList"
ItemsSource="{Binding Source={StaticResource StationListSorted}}"
SelectedItem="{Binding SelectedStation, Mode=TwoWay}"
>
StationListSorted 是作为 CollectionViewSource 的 StationList 上的一个简单的单一属性排序.现在事情变得丑陋了!!同一个视图在列表框中加载了相同的项目,但现在正确排序,但第一个项目被选中并设置了 selectedItem 属性!!
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!!
如何使用 CollectionViewSource 对 ListBox 进行排序而不自动选择我的第一项?
How can I sort a ListBox with a CollectionViewSource WITHOUT it auto selecting my first item?
推荐答案
在您的列表框中,尝试设置 IsSynchronizedWithCurrentItem
并查看哪个值(true 或 false)会产生所需的效果.
On your listbox, try setting IsSynchronizedWithCurrentItem
and see which value (either true or false) produces the desired effect.
这篇关于SelectedItem 使用 CollectionViewSource 设置为第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!