/// <summary>
        /// Create model for FilterControl, if it is possible in current state.
        /// </summary>
        /// <returns>Instance of FilterControlVm or null.</returns>
        protected virtual FilterControlVm CreateModel()
        {
            FilterControlVm vm = null;

            filterPresenter = Parent == null ? null : FilterPresenter.TryGet(ParentCollection);
            if (filterPresenter != null)
            {
                vm = filterPresenter.TryGetFilterControlVm(Key, FilterInitializersManager);
            }
            return(vm);
        }
        private static void OnIsActiveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is FilterControl filterControl)
            {
                bool newValue = Convert.ToBoolean(e.NewValue);
                bool oldValue = Convert.ToBoolean(e.OldValue);
                if (newValue == false)
                {
                    FilterControls.Remove(filterControl);
                    filterControl.Unloaded -= FilterUnloaded;
                    if (filterControl.ViewModel != null)
                    {
                        filterControl.ViewModel.StateChanged  -= ViewModelStateChanged;
                        filterControl.ViewModel.FilterChanged -= ViewModelFilterChanged;
                    }
                }
                else
                {
                    FilterControls.Add(filterControl);
                    filterControl.Loaded   += FilterLoaded;
                    filterControl.Unloaded += FilterUnloaded;
                }

                return;
            }

            if (d is IFilterView filterView)
            {
                bool newValue = Convert.ToBoolean(e.NewValue);
                bool oldValue = Convert.ToBoolean(e.OldValue);
                if (newValue == false)
                {
                    filterView.ViewModelChanged -= FilterView_ViewModelChanged;
                    if (filterView.ViewModel != null)
                    {
                        FilterPresenter filterPresenter = filterView.ViewModel.FilterPresenter;
                        FilterPresenters.Remove(filterPresenter);
                        filterPresenter.Filtered -= FilterPresenter_Filter;
                    }
                }
                else
                {
                    filterView.ViewModelChanged += FilterView_ViewModelChanged;
                    if (filterView.ViewModel != null)
                    {
                        FilterPresenter filterPresenter = filterView.ViewModel.FilterPresenter;
                        FilterPresenters.Add(filterPresenter);
                        filterPresenter.Filtered += FilterPresenter_Filter;
                    }
                }
            }
        }
        // <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);
        }
Beispiel #4
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);
        }
        private static void FilterView_ViewModelChanged(object sender, ViewModelChangedEventArgs e)
        {
            var oldViewModel = e.OldViewModel;

            if (oldViewModel != null)
            {
                FilterPresenter filterPresenter = oldViewModel.FilterPresenter;
                FilterPresenters.Remove(filterPresenter);
                filterPresenter.Filtered -= FilterPresenter_Filter;
            }
            var newViewModel = e.NewViewModel;

            if (newViewModel != null)
            {
                FilterPresenter filterPresenter = newViewModel.FilterPresenter;
                FilterPresenters.Add(filterPresenter);
                filterPresenter.Filtered += FilterPresenter_Filter;
            }
        }
            internal DisposeItemsDeferRefresh(FilterPresenter filterVm)
            {
                this.filterPr = filterVm;
                IEditableCollectionView cv = filterPr.CollectionView as IEditableCollectionView;

                if (cv != null)
                {
                    if (cv.IsAddingNew)
                    {
                        cv.CommitNew();
                    }
                    if (cv.IsEditingItem)
                    {
                        cv.CommitEdit();
                    }
                }
                if (filterPr.itemsDeferRefreshCount == 0)
                {
                    filterPr.itemsDeferRefresh = filterPr.CollectionView.DeferRefresh();
                }
                filterPr.itemsDeferRefreshCount++;
            }