Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Silverlight中为ObservableCollection创建CollectionView_C#_Silverlight - Fatal编程技术网

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

C# 如何在Silverlight中为ObservableCollection创建CollectionView,c#,silverlight,C#,Silverlight,我正在与silverlight合作,我想申请一个ObservableCollection 所以我开始查看ICollectionView,因为Silverlight中没有CollectionViewSource,它包含大量的方法和事件。我搜索了一段时间,不知道是否有人有ICollectionView实现的示例代码?不幸的是,ICollectionView仅用于Silverlight 2.0中的DataGrid,其唯一实现是ListCollectionView,它是System.Windows.Co

我正在与silverlight合作,我想申请一个ObservableCollection


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

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

如果您没有绑定到DataGrid,ICollectionView将不会给您太多帮助,因为据我所知,listbox等基本控件没有使用它,因为它是在数据控件程序集中定义的,而不是在核心中定义的

这与WPF有很大区别


但就您的问题而言,包含DataGrid的程序集确实有一个实现,如果您想了解它是如何实现的,它可能会帮助您。最糟糕的情况是,reflector是您的朋友…

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

另一种方法是在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"));
            }
        }
    }
}

CollectionViewSource现在在Silverlight 3中提供。查看一篇关于这方面的好文章。

在ViewModel CLR对象中是否有LINQ示例?您不必每次修改名称时调用UpdateFilteredStores,LINQ会自动重新计算请求。