예제 #1
0
        void EnsureView(object source, Type collectionViewType)
        {
            if (_isInitializing || _deferLevel > 0)
            {
                return;
            }

            DataSourceProvider dataProvider = source as DataSourceProvider;

            // listen for DataChanged events from an DataSourceProvider
            if (dataProvider != _dataProvider)
            {
                if (_dataProvider != null)
                {
                    DataChangedEventManager.RemoveHandler(_dataProvider, OnDataChanged);
                }

                _dataProvider = dataProvider;

                if (_dataProvider != null)
                {
                    DataChangedEventManager.AddHandler(_dataProvider, OnDataChanged);
                    _dataProvider.InitialLoad();
                }
            }

            // if the source is DataSourceProvider, use its Data instead
            if (dataProvider != null)
            {
                source = dataProvider.Data;
            }

            // get the view
            ICollectionView view = null;

            if (source != null)
            {
                DataBindEngine engine     = DataBindEngine.CurrentDataBindEngine;
                ViewRecord     viewRecord = engine.GetViewRecord(source, this, collectionViewType, true,
                                                                 (object x) =>
                {
                    BindingExpressionBase beb = BindingOperations.GetBindingExpressionBase(this, SourceProperty);
                    return((beb != null) ? beb.GetSourceItem(x) : null);
                });

                if (viewRecord != null)
                {
                    view = viewRecord.View;
                    _isViewInitialized = viewRecord.IsInitialized;

                    // bring view up to date with the CollectionViewSource
                    if (_version != viewRecord.Version)
                    {
                        ApplyPropertiesToView(view);
                        viewRecord.Version = _version;
                    }
                }
            }

            // update the View property
            SetValue(ViewPropertyKey, view);
        }