对 CollectionViewSource 感到困惑(SelectedItem 在组合中不起作用) [英] Confused about CollectionViewSource (SelectedItem not working in combos)

查看:22
本文介绍了对 CollectionViewSource 感到困惑(SelectedItem 在组合中不起作用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆组合,它们都有相同的可用选项.这些选择在从我的 ViewModel 公开的集合中提供.一切都很好.

I have a bunch of combos that all share the same available choices. These choices are provided in a collection exposed from my ViewModel. All fine and dandy.

我现在希望对这些选项进行排序,因此我决定从我的 ViewModel 中公开一个 ICollectionView 而不是我通常的 ReadonlyObservableCollection,并在我的视图中对集合视图进行排序视图模型.

I now want these choices sorted, so I decided to expose an ICollectionView from my ViewModel instead of my usual ReadonlyObservableCollection<T>, and sort the collection view in my ViewModel.

class EditStuffViewModel : ViewModelBase
{
    public EditStuffViewModel (ObservableCollection<Choice> choices)
    {
        Choices = new CollectionViewSource() { Source = choices }.View;
        Choices.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
    }

    public ICollectionView Choices
    {
        get;
        private set;
    }

    //snip other properties
}

这一切都很好,只是现在我所有的组合现在都同步了他们的选择.

This all works fine except that now all my combos now sync their selection.

这不是我想要的.我希望共享选择,但选择要与其正常绑定.我想我知道我的 CollectionView 正在跟踪选择,但我认为这是为每个控件选择的行为.

This is not what I want. I want the choices to be shared, but selections to be to their normal bindings. I think I understand that my CollectionView is tracking selection, but I thought this was behaviour was opt in for each control.

我尝试在我的组合上显式设置 IsSynchronizedWithCurrentItem="False" 成功地将它们解耦,但是我绑定的 SelectedItem 从未在组合中选择(ViewModel 的绑定调用 getter 但从未选择结果).选择一个项目似乎可以正确更新我的 ViewModel 的 setter.

I have tried explicitly setting IsSynchronizedWithCurrentItem="False" on my combos which successfully decouples them, but then my bound SelectedItem is never selected in the combo (the ViewModel's bound getter is called but the result is never selected). Selecting an item does seem to update my ViewModel's setter correctly.

我显然遗漏了 CollectionView 应该如何工作的一些基本知识.任何人都可以启发我吗?

I'm obviously missing something fundamental to how CollectionView is supposed to work. Can anyone enlighten me?

编辑:糟糕,这确实适用于IsSynchronizedWithCurrentItem="False".详情请看我的回答.

EDIT: My bad, this DOES work with IsSynchronizedWithCurrentItem="False". See my answer for details.

干杯.

推荐答案

很抱歉浪费大家的时间,但是设置 IsSynchronizedWithCurrentItem="False" 确实有效.我还添加了一个过滤器以及排序,默认选择的值不在过滤的项目列表中.糟糕.

I'm very sorry for wasting everyone's time, but setting IsSynchronizedWithCurrentItem="False" does work. I had also added a filter along with the sort, and the default selected values were not in the filtered list of items. Oops.

至于为什么我需要明确关闭 IsSynchronizedWithCurrentItem 而我通常不会在标准集合上这样做时,MSDN

As for WHY I needed to explicitly turn IsSynchronizedWithCurrentItem off when I never normally do on standard collections, light is shed on MSDN

true 如果 SelectedItem 始终与 ItemCollection 中的当前项同步;false 如果 SelectedItem 从未与当前项目同步;仅当 Selector 使用 CollectionView 时 SelectedItem 才与当前项目同步.默认值为Nothing.

true if the SelectedItem is always synchronized with the current item in the ItemCollection; false if the SelectedItem is never synchronized with the current item; Nothing if the SelectedItem is synchronized with the current item only if the Selector uses a CollectionView. The default value is Nothing.

换句话说,如果您显式使用 CollectionView 而不是在普通集合上使用默认视图,您将获得选择同步.

So in other words, if you use a CollectionView explicitly rather than using the default view on a normal collection, you get selection sync.

这篇关于对 CollectionViewSource 感到困惑(SelectedItem 在组合中不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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