示例#1
0
        /// <summary>
        /// Initializes the filter view model.
        /// </summary>
        protected override FilterControlVm CreateModel()
        {
            FilterControlVm vm = null;

            filterPresenter = Parent == null ? null : FilterPresenter.TryGet(ParentCollection);
            if (filterPresenter != null)
            {
                if (Key == null)
                {
                    DataGridColumnHeader columnHeader = this.GetParent <DataGridColumnHeader>();
                    if (columnHeader == null)
                    {
                        return(null);
                    }
                    DataGridColumn column = columnHeader.Column;
                    if (column == null)
                    {
                        return(null);
                    }
                    DataGrid dataGrid = columnHeader.GetParent <DataGrid>();
                    if (dataGrid == null)
                    {
                        return(null);
                    }
                    if (column.DisplayIndex >= dataGrid.Columns.Count)
                    {
                        return(null);
                    }
                    IEnumerable <FilterInitializer> initializers = GetInitializers(column) ?? FilterInitializersManager;
                    string key = Key ?? GetColumnKey(column);
                    vm = filterPresenter.TryGetFilterControlVm(key, initializers);
                }
                else
                {
                    IEnumerable <FilterInitializer> initializers = FilterInitializersManager;
                    vm = filterPresenter.TryGetFilterControlVm(Key, initializers);
                }
                if (vm != null)
                {
                    vm.IsEnable = true;
                }
            }
            return(vm);
        }
        // <summary>
        // Returns FilterPresenter, connected to a pass source .
        // If pass instance of ICollectionView, FilterPresenter connected to passed instance, otherwise, filterPresenter connected to default view for passed collection.
        // </summary>
        // <param name="source">ICollectionView for source or source</param>
        // <returns>FilterPresenter, connected to source, or null if source is null.</returns>
        public static FilterPresenter TryGet(IEnumerable source)
        {
            if (source == null)
            {
                return(null);
            }
            ICollectionView sourceCollectionView = source as ICollectionView;

            if (sourceCollectionView == null)
            {
                sourceCollectionView = CollectionViewSource.GetDefaultView(source);
            }
            FilterPresenter instance = null;

            //GC.Collect();
            foreach (var entry in filterPresenters.ToArray())
            {
                if (!entry.Value.IsAlive)
                {
                    filterPresenters.Remove(entry.Key);
                }
            }
            if (filterPresenters.ContainsKey(sourceCollectionView))
            {
                var wr = filterPresenters[sourceCollectionView];
                instance = wr.Target as FilterPresenter;
            }
            if (instance == null)
            {
                instance = new FilterPresenter(sourceCollectionView);
                if (filterPresenters.ContainsKey(sourceCollectionView))
                {
                    filterPresenters[sourceCollectionView] = new WeakReference(instance);
                }
                else
                {
                    filterPresenters.Add(sourceCollectionView, new WeakReference(instance));
                }
            }
            return(instance);
        }