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

问题描述

我有一个视图数据通过 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 是 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 设置为第一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 06:48