예제 #1
0
        private ViewRecord GetViewRecord(object source)
        {
            // Order of precendence in acquiring the View:
            // 1) If the CollectionView for this collection has been cached, then
            //    return the cached instance.
            // 2) If the collection is an ICollectionViewFactory use ICVF.CreateView()
            //    from the collection
            // 3) If the collection is an IList return a new ListCollectionView
            // 4) If the collection is an IEnumerable, return a new EnumerableCollectionView
            // 5) return null

            // if the view already exists, just return it
            ViewRecord viewRecord = GetExistingViewRecord(source);

            if (viewRecord != null)
            {
                return(viewRecord);
            }

            ICollectionView icv = null;

            ICollectionViewFactory icvf = source as ICollectionViewFactory;

            if (icvf != null)
            {
                // collection is a view factory - call its factory method
                icv = icvf.CreateView();
            }
            else
            {
                // collection is not a factory - create an appropriate view
                IList il = source as IList;
                if (il != null)
                {
                    icv = new ListCollectionView(il);
                }
                else
                {
                    // collection is not IList, wrap it
                    IEnumerable ie = source as IEnumerable;
                    if (ie != null)
                    {
                        icv = new EnumerableCollectionView(ie);
                    }
                }
            }

            // if we got a view, add it to the tables
            if (icv != null)
            {
                viewRecord = new ViewRecord(icv);
                this.CachedViews[source] = viewRecord;
            }

            return(viewRecord);
        }
예제 #2
0
        /// <summary>
        /// Creates a collection view around the DataGrid's source. ICollectionViewFactory is
        /// used if the source implements it. Otherwise a PagedCollectionView is returned.
        /// </summary>
        /// <param name="source">Enumerable source for which to create a view</param>
        /// <returns>ICollectionView view over the provided source</returns>
#else
        /// <summary>
        /// Creates a collection view around the DataGrid's source. ICollectionViewFactory is
        /// used if the source implements it.
        /// </summary>
        /// <param name="source">Enumerable source for which to create a view</param>
        /// <returns>ICollectionView view over the provided source</returns>
#endif
        internal static ICollectionView CreateView(IEnumerable source)
        {
            Debug.Assert(source != null, "source unexpectedly null");
            Debug.Assert(!(source is ICollectionView), "source is an ICollectionView");

            ICollectionView collectionView = null;

            ICollectionViewFactory collectionViewFactory = source as ICollectionViewFactory;

            if (collectionViewFactory != null)
            {
                // If the source is a collection view factory, give it a chance to produce a custom collection view.
                collectionView = collectionViewFactory.CreateView();

                // Intentionally not catching potential exception thrown by ICollectionViewFactory.CreateView().
            }

#if FEATURE_PAGEDCOLLECTIONVIEW
            if (collectionView == null)
            {
                collectionView = new PagedCollectionView(source);
            }
#endif

            if (collectionView == null)
            {
                IList sourceAsList = source as IList;
                if (sourceAsList != null)
                {
                    collectionView = new ListCollectionView(sourceAsList);
                }
                else
                {
                    collectionView = new EnumerableCollectionView(source);
                }
            }

            return(collectionView);
        }