与CollectionViewSource绑定 [英] Binding with CollectionViewSource

查看:118
本文介绍了与CollectionViewSource绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一些组合框的使用CollectionViewSource排序。该组合框实际上是一个数据模板的一部分,并重复在列表视图。我的第一种方法似乎(使用CollectionViewSource)工作,但我所有的组合框共享同一个数据上下文。这使得它如此每当其他框改为所有其他一种方法来改变,以反映 - 而不是期望的副作用

I am trying to implement some combo box sorting using CollectionViewSource. This combo box is actually part of a data template and is repeated in a list view. My first approach seemed to work (using CollectionViewSource) but all my combo boxes shared the same data context. This made it so whenever one of the other boxes was changed all the others changed to reflect - not a desired side effect.

我决定只拉了回来,并尝试实施一个基本的组合框(不是数据模板中)使用内联XAML用于指定CollectionViewSource(而不是创建CVS作为静态资源)。我一直没能顺利拿到我要显示的数据。 。我可能会对此完全错误的,因为我还在新的WPF

I decided to just pull back and try to implement a basic combo box (not inside a data template) using inline xaml for specifying the CollectionViewSource (as opposed to creating the cvs as a static resource). I have not been able to successfully get my data to display. I'm probably going about this entirely wrong as I'm still new to WPF.

下面是我的组合框的XAML:

Here is the xaml for my combo box:

<ComboBox>
    <ComboBox.ItemsSource>
        <Binding>
            <Binding.Source>
                <CollectionViewSource Source="{Binding Path=Configurations}">
                    <CollectionViewSource.SortDescriptions>
                        <scm:SortDescription PropertyName="AgencyName" />
                    </CollectionViewSource.SortDescriptions>
                </CollectionViewSource>
            </Binding.Source>
        </Binding>
    </ComboBox.ItemsSource>
</ComboBox>



,其中该组合框住,必然有所谓的一个ObservableCollection对象的用户控件的DataContext配置和每个配置有一个名为AgencyName属性。我已经验证了这工作正常使用标准,而不CVS结合,所以我知道一切都是在一致的罚款。

The DataContext of the user control where this combo box lives is bound to an object which has an ObservableCollection called Configurations and each configuration has a property called AgencyName. I've verified that this works fine using standard binding without the cvs so I know everything is fine in that accord.

任何帮助将不胜感激,因为我已经跑了出来的借口,我的老板:)。我也不想有向下拖放到代码,做后面的代码排序(我可以当我建立的ObservableCollection但恕我直言,违反了DRY原则)。

Any help would be greatly appreciated as I have ran out of excuses to my boss :). I also don't want to have to drop down into code and do the sorting in the code behind (which I could when I build the ObservableCollection but IMHO that violates the DRY principle).

推荐答案

究竟你的意思是什么?每当其他框之一改变所有其它改变,以反映?你在谈论的SelectedItem?
。如果是这样,那么它可以帮助你的组合框设置 IsSynchronizedWithCurrentItem = FALSE

What exactly do you mean by "whenever one of the other boxes was changed all the others changed to reflect"? Are you talking about the SelectedItem? If so, then it may help setting IsSynchronizedWithCurrentItem = false in your ComboBox.

在那旁边:我觉得只要你创建和后面的代码进行排序您的ICollectionView只有一次,没有违反DRY原则,因为你做的更多的是有没有在XAML需要了。但我看到有可能是其他原因说,像排序功能应在视图中进行,在模型 - 视图 - 视图模型来说的。

Beside that: I think as long as you create and sort your ICollectionView in the code behind only once, there is no violation of the DRY principle, because what you do more there is not needed in the XAML anymore. But I see there may be other reasons to say that a feature like sorting should be done in the View, speaking in terms of Model-View-ViewModel.

这篇关于与CollectionViewSource绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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