SelectedItem设置为具有CollectionViewSource的第一个项目 [英] SelectedItem set to first item with CollectionViewSource

查看:61
本文介绍了SelectedItem设置为具有CollectionViewSource的第一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过mvvm light绑定到WP7项目中的viewmodel的视图数据. 该视图包含具有以下设置的列表框:

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是StationList上的一个简单属性,它作为CollectionViewSource进行排序. 现在事情变得丑陋了! 相同的视图在列表框中加载了相同的项目,但现在已正确排序,但是选择了第一个项目并设置了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并查看哪个值(正确或错误)产生所需的效果.

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

这篇关于SelectedItem设置为具有CollectionViewSource的第一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆