混淆关于CollectionViewSource(SelectedItem不工作在组合) [英] Confused about CollectionViewSource (SelectedItem not working in combos)

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

问题描述

我有一个组合,所有共享相同的可用选择。这些选择在从我的ViewModel公开的集合中提供。所有罚款和dandy。



我现在想要这些选择排序,所以我决定公开一个 ICollectionView 从我的ViewModel而不是我通常的 ReadonlyObservableCollection< T> ,并在我的ViewModel中排序集合视图。

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

public ICollectionView选择
{
get;
private set;
}

// snip其他属性
}

这一切都正常,除了现在所有我的组合现在同步他们的选择



这不是我想要的。我想要的选择是共享,但选择是他们的正常绑定。我想我明白我的CollectionView是跟踪选择,但我认为这是行为是选择加入每个控件。



我已经尝试显式设置 IsSynchronizedWithCurrentItem =False在我的组合成功解耦他们,但然后我的绑定 SelectedItem 从未在组合中选择(ViewModel的绑定getter是调用但结果从未选择)。选择一个项目似乎更新我的ViewModel的设置正确。



我显然缺少一些基本的一些基本的CollectionView应该工作。任何人都可以启发我?



EDIT :我的错误, DOES 使用 IsSynchronizedWithCurrentItem =False / code>。

解决方案

I'非常抱歉浪费大家的时间,但设置 IsSynchronizedWithCurrentItem =False可以工作。我还添加了一个过滤器以及排序,默认选择的值不在过滤的项目列表中。糟糕。



至于WHY我需要明确地关闭IsSynchronizedWithCurrentItem关闭,当我从来没有正常做标准集合,光是在 MSDN


true 如果SelectedItem总是与ItemCollection中的当前项同步; false (如果SelectedItem永远不与当前项同步) 如果SelectedItem与当前项目同步,则仅当Selector使用CollectionView时才会显示。


因此,换句话说,如果使用 CollectionView ,而不是在正常集合中使用默认视图,则会获得选择同步。


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.

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.

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.

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.

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

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

Cheers.

解决方案

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.

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

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.

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天全站免登陆