C# 如何在 Silverlight 中为 ObservableCollection<T> 创建 CollectionView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/671818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:44:05  来源:igfitidea点击:

How to create a CollectionView for ObservableCollection<T> in Silverlight

c#silverlight

提问by Emil C

I'm working with silverlight and I want to filer an ObservableCollection.

我正在使用 Silverlight,并且我想提交一个 ObservableCollection。

So I started to look att ICollectionView, because there is no CollectionViewSource in Silverlight and it contains an absure amount of methods and events. I've searched for a while and I wonder if anyone has example code of an implementation of ICollectionView?

所以我开始关注ICollectionView,因为Silverlight 中没有CollectionViewSource 并且它包含了大量的方法和事件。我已经搜索了一段时间,我想知道是否有人有 ICollectionView 实现的示例代码?

采纳答案by Denis Troller

Unfortunately ICollectionView is only used for the DataGrid in Silverlight 2.0, and its only implementation is ListCollectionView, which is internal to System.Windows.Controls.Data.

不幸的是,ICollectionView 仅用于 Silverlight 2.0 中的 DataGrid,并且它的唯一实现是 ListCollectionView,它是 System.Windows.Controls.Data 内部的。

If you are not binding to a DataGrid, ICollectionView will not give you much because it is not used by the basic controls (such as listbox) as far as I can tell, since it is defined in the Data controls assembly and not in the core.

如果你没有绑定到 DataGrid,ICollectionView 不会给你太多,因为据我所知,它没有被基本控件(例如列表框)使用,因为它是在数据控件程序集中定义的,而不是在核心中.

This is a pretty big difference with WPF.

这与 WPF 有很大的不同。

But to the point of your question, the assembly containing the DataGrid does have an implementation that might help you if you want to learn how it is done. Worst case, reflector is your friend...

但是就您的问题而言,包含 DataGrid 的程序集确实有一个实现,如果您想了解它是如何完成的,可能会对您有所帮助。最坏的情况,反射器是你的朋友......

回答by Michael S. Scherotter

One method would be to use a Value Converter if you want to do data binding to the ObservableCollection.

如果要将数据绑定到 ObservableCollection,一种方法是使用值转换器。

Another method would be to use LINQ in a ViewModel CLR object that would do the filtering based on properties in the ViewModel like this (see the implementation method UpdateFilteredStores() at the bottom):

另一种方法是在 ViewModel CLR 对象中使用 LINQ,该对象将根据 ViewModel 中的属性进行过滤,如下所示(请参阅底部的实现方法 UpdateFilteredStores()):

namespace UnitTests
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Linq;

    public class ViewModel : INotifyPropertyChanged
    {
        private string name;

        public ViewModel()
        {
            this.Stores = new ObservableCollection<string>();

            this.Stores.CollectionChanged += new NotifyCollectionChangedEventHandler(this.Stores_CollectionChanged);

            // TODO: Add code to retreive the stores collection
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public ObservableCollection<string> Stores { get; private set; }

        public IEnumerable<string> FilteredStores { get; private set; }

        public string Name 
        { 
            get
            {
                return this.name;
            }

            set
            {
                this.name = value;

                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                }

                this.UpdateFilteredStores();
            }
        }

        private void Stores_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            this.UpdateFilteredStores();
        }

        private void UpdateFilteredStores()
        {
            this.FilteredStores = from store in this.Stores
                                  where store.Contains(this.Name)
                                  select store;

            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("FilteredStores"));
            }
        }
    }
}

回答by Andy May

CollectionViewSource is now available in Silverlight 3. Check out a good article about this here.

CollectionViewSource 现在在 Silverlight 3 中可用。请在此处查看有关此内容的好文章。