Exemplo n.º 1
0
        void CreateView(object source)
        {
            if (source == null)
            {
                View = null;
            }
            else
            {
                ICollectionViewFactory factory = source as ICollectionViewFactory;
                if (factory != null)
                {
                    View = factory.CreateView();
                    if (View == null)
                    {
                        throw new InvalidOperationException(string.Format("ICollectionViewFactory.CreateView must not return null", source.GetType().Name));
                    }
                }
                else
                {
                    ICollectionView view = null;
                    if (CachedViews.TryGetValue(source, out view))
                    {
                        View = view;
                    }
                    else
                    {
                        view = CollectionView.Create((IEnumerable)source);
                        CachedViews.Add(source, view);
                        View = view;
                    }
                }
            }

            Refresh();
        }
        /// <summary>
        /// Converts an <see cref="IEnumerable{T}"/> to an <see cref="EntityCollectionView{T}"/> if
        /// the type is an <see cref="Entity"/> type.
        /// </summary>
        /// <param name="value">The source data being passed to the target.</param>
        /// <param name="targetType">The System.Type of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the target dependency property.</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || value is ICollectionView)
            {
                return(value);
            }

            ICollectionViewFactory factory = value as ICollectionViewFactory;

            if (factory != null)
            {
                return(factory.CreateView());
            }

            // Determine if the value is an IEnumerable<T>
            Type entityType;

            if (IsEnumerableEntityType(value.GetType(), out entityType))
            {
                // Create the appropriate EntityCollectionView<T> type
                Type viewType = typeof(PagedEntityCollectionView <>).MakeGenericType(entityType);

                // Use the constructor that accepts the IEnumerable<T> source to construct the EntityCollectionView
                return(Activator.CreateInstance(viewType, value));
            }

            return(value);
        }
Exemplo n.º 3
0
        private ICollectionView CreateNewView(object collection)
        {
            ICollectionViewFactory collectionViewFactory = collection as ICollectionViewFactory;

            if (collectionViewFactory != null)
            {
                ICollectionView collectionViews = collectionViewFactory.CreateView();
                if (collectionViews == null)
                {
                    throw new InvalidOperationException("Unsupported Null Collection View");
                }
                if (collectionViews is IViewLifetime)
                {
                    return(collectionViews);
                }
                return(new CollectionViewProxy(collectionViews));
            }
            IList lists = collection as IList;

            if (lists != null)
            {
                return(new ListCollectionView(lists));
            }
            IEnumerable enumerable = collection as IEnumerable;

            if (enumerable == null)
            {
                throw new InvalidOperationException("Collection View Unsupported Source Type");
            }
            return(new EnumerableCollectionView(enumerable));
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
        /// <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>
        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 (collectionView == null)
            {
                // If we still do not have a collection view, default to a PagedCollectionView.
                collectionView = new PagedCollectionView(source);
            }
            return(collectionView);
        }
Exemplo n.º 7
0
        static ICollectionView BuildView(object source)
        {
            if ((source is IEnumerable) && !((IEnumerable)source).GetEnumerator().MoveNext())
            {
                return(null);
            }
            ICollectionView view = source as ICollectionView;

            if (view != null)
            {
                return(view);
            }
            ICollectionViewFactory factory = source as ICollectionViewFactory;

            if (factory != null)
            {
                return(factory.CreateView());
            }
            var viewSource = new CollectionViewSource();

            viewSource.Source = source;
            view = viewSource.View;
            return(view);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Return the object associated with (collection, cvs, type).
        /// If this is the first reference to this view, add it to the tables.
        /// </summary>
        /// <exception cref="ArgumentException">
        /// Thrown when the collectionViewType does not implement ICollectionView
        /// or does not have a constructor that accepts the type of collection.
        /// Also thrown when the named collection view already exists and is
        /// not the specified collectionViewType.
        /// </exception>
        internal ViewRecord GetViewRecord(object collection, CollectionViewSource cvs, Type collectionViewType, bool createView, Func <object, object> GetSourceItem)
        {
            // Order of precendence in acquiring the View:
            // 0) If  collection is already a CollectionView, return it.
            // 1) If the CollectionView for this collection has been cached, then
            //    return the cached instance.
            // 2) If a CollectionView derived type has been passed in collectionViewType
            //    create an instance of that Type
            // 3) If the collection is an ICollectionViewFactory use ICVF.CreateView()
            //    from the collection
            // 4) If the collection is an IListSource call GetList() and perform 5),
            //    etc. on the returned list
            // 5) If the collection is an IBindingList return a new BindingListCollectionView
            // 6) If the collection is an IList return a new ListCollectionView
            // 7) If the collection is an IEnumerable, return a new CollectionView
            //    (it uses the ListEnumerable wrapper)
            // 8) return null
            // An IListSource must share the view with its underlying list.

            // if the view already exists, just return it
            // Also, return null if it doesn't exist and we're called in "lazy" mode
            ViewRecord viewRecord = GetExistingView(collection, cvs, collectionViewType, GetSourceItem);

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

            // If the collection is an IListSource, it uses the same view as its
            // underlying list.
            IListSource ils     = collection as IListSource;
            IList       ilsList = null;

            if (ils != null)
            {
                ilsList    = ils.GetList();
                viewRecord = GetExistingView(ilsList, cvs, collectionViewType, GetSourceItem);

                if (viewRecord != null)
                {
                    return(CacheView(collection, cvs, (CollectionView)viewRecord.View, viewRecord));
                }
            }

            // Create a new view
            ICollectionView icv = collection as ICollectionView;

            if (icv != null)
            {
                icv = new CollectionViewProxy(icv);
            }
            else if (collectionViewType == null)
            {
                // Caller didn't specify a type for the view.
                ICollectionViewFactory icvf = collection 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 = (ilsList != null) ? ilsList : collection as IList;
                    if (il != null)
                    {
                        // create a view on an IList or IBindingList
                        IBindingList ibl = il as IBindingList;
                        if (ibl != null)
                        {
                            icv = new BindingListCollectionView(ibl);
                        }
                        else
                        {
                            icv = new ListCollectionView(il);
                        }
                    }
                    else
                    {
                        // collection is not IList, wrap it
                        IEnumerable ie = collection as IEnumerable;
                        if (ie != null)
                        {
                            icv = new EnumerableCollectionView(ie);
                        }
                    }
                }
            }
            else
            {
                // caller specified a type for the view.  Try to honor it.
                if (!typeof(ICollectionView).IsAssignableFrom(collectionViewType))
                {
                    throw new ArgumentException(SR.Get(SRID.CollectionView_WrongType, collectionViewType.Name));
                }

                // if collection is IListSource, get its list first (
                object arg = (ilsList != null) ? ilsList : collection;

                try
                {
                    icv = Activator.CreateInstance(collectionViewType,
                                                   System.Reflection.BindingFlags.CreateInstance, null,
                                                   new object[1] {
                        arg
                    }, null) as ICollectionView;
                }
                catch (MissingMethodException e)
                {
                    throw new ArgumentException(SR.Get(SRID.CollectionView_ViewTypeInsufficient,
                                                       collectionViewType.Name, collection.GetType()), e);
                }
            }

            // if we got a view, add it to the tables
            if (icv != null)
            {
                // if the view doesn't derive from CollectionView, create a proxy that does
                CollectionView cv = icv as CollectionView;
                if (cv == null)
                {
                    cv = new CollectionViewProxy(icv);
                }

                if (ilsList != null)    // IListSource's list shares the same view
                {
                    viewRecord = CacheView(ilsList, cvs, cv, null);
                }

                viewRecord = CacheView(collection, cvs, cv, viewRecord);

                // raise the event for a new view
                BindingOperations.OnCollectionViewRegistering(cv);
            }

            return(viewRecord);
        }
        // Token: 0x060076F8 RID: 30456 RVA: 0x0021FCC8 File Offset: 0x0021DEC8
        internal ViewRecord GetViewRecord(object collection, CollectionViewSource cvs, Type collectionViewType, bool createView, Func <object, object> GetSourceItem)
        {
            ViewRecord viewRecord = this.GetExistingView(collection, cvs, collectionViewType, GetSourceItem);

            if (viewRecord != null || !createView)
            {
                return(viewRecord);
            }
            IListSource listSource = collection as IListSource;
            IList       list       = null;

            if (listSource != null)
            {
                list       = listSource.GetList();
                viewRecord = this.GetExistingView(list, cvs, collectionViewType, GetSourceItem);
                if (viewRecord != null)
                {
                    return(this.CacheView(collection, cvs, (CollectionView)viewRecord.View, viewRecord));
                }
            }
            ICollectionView collectionView = collection as ICollectionView;

            if (collectionView != null)
            {
                collectionView = new CollectionViewProxy(collectionView);
            }
            else if (collectionViewType == null)
            {
                ICollectionViewFactory collectionViewFactory = collection as ICollectionViewFactory;
                if (collectionViewFactory != null)
                {
                    collectionView = collectionViewFactory.CreateView();
                }
                else
                {
                    IList list2 = (list != null) ? list : (collection as IList);
                    if (list2 != null)
                    {
                        IBindingList bindingList = list2 as IBindingList;
                        if (bindingList != null)
                        {
                            collectionView = new BindingListCollectionView(bindingList);
                        }
                        else
                        {
                            collectionView = new ListCollectionView(list2);
                        }
                    }
                    else
                    {
                        IEnumerable enumerable = collection as IEnumerable;
                        if (enumerable != null)
                        {
                            collectionView = new EnumerableCollectionView(enumerable);
                        }
                    }
                }
            }
            else
            {
                if (!typeof(ICollectionView).IsAssignableFrom(collectionViewType))
                {
                    throw new ArgumentException(SR.Get("CollectionView_WrongType", new object[]
                    {
                        collectionViewType.Name
                    }));
                }
                object obj = (list != null) ? list : collection;
                try
                {
                    collectionView = (Activator.CreateInstance(collectionViewType, BindingFlags.CreateInstance, null, new object[]
                    {
                        obj
                    }, null) as ICollectionView);
                }
                catch (MissingMethodException innerException)
                {
                    throw new ArgumentException(SR.Get("CollectionView_ViewTypeInsufficient", new object[]
                    {
                        collectionViewType.Name,
                        collection.GetType()
                    }), innerException);
                }
            }
            if (collectionView != null)
            {
                CollectionView collectionView2 = collectionView as CollectionView;
                if (collectionView2 == null)
                {
                    collectionView2 = new CollectionViewProxy(collectionView);
                }
                if (list != null)
                {
                    viewRecord = this.CacheView(list, cvs, collectionView2, null);
                }
                viewRecord = this.CacheView(collection, cvs, collectionView2, viewRecord);
                BindingOperations.OnCollectionViewRegistering(collectionView2);
            }
            return(viewRecord);
        }