コード例 #1
0
        public void FilterCollection()
        {
            if (FilterValue == null || FilterValue.Equals(default(TInner)))
            {
                FilteredCollection.Clear();
                return;
            }

            foreach (var item in UnfilteredCollection)
            {
                if (PassFilterDelegate(item, FilterValue))
                {
                    if (!FilteredCollection.Contains(item))
                    {
                        FilteredCollection.Add(item);
                    }
                }
                else
                {
                    if (FilteredCollection.Contains(item))
                    {
                        FilteredCollection.Remove(item);
                    }
                }
            }
        }
コード例 #2
0
        private async void RunFilter()
        {
            _delay?.Cancel();

            _delay = new CancellationTokenSource();
            try
            {
                await Task.Delay(500, _delay.Token);
            }
            catch (TaskCanceledException)
            {
                // request was canceled, return;
                return;
            }

            var currentText = _searchBox?.Text ?? string.Empty;

            var sourceCollection = SourceCollection.Cast <FileContainer>().ToList();

            if (string.IsNullOrWhiteSpace(currentText))
            {
                // When nothing is being filtered, we dont need to filter any thing.
                FilteredCollection.Clear();
                FilteredCollection.AddRange(sourceCollection, true);
            }
            else
            {
                // Remove any items in the filtered collection that are no longer available in the primary collection
                var oldItems = (from x in FilteredCollection where !sourceCollection.Contains(x) select x).ToList();
                oldItems.ForEach(x => FilteredCollection.Remove(x));

                // look at existing filtered items and remove those that do not match the filter
                oldItems = (from x in FilteredCollection where !DoesContainString(x, currentText) select x).ToList();
                oldItems.ForEach(x => FilteredCollection.Remove(x));

                // Look at the source colleciton and find out what needs to be in the filtered collection
                var newItems = (from x in sourceCollection where DoesContainString(x, currentText) && !FilteredCollection.Contains(x) select x).ToList();
                FilteredCollection.AddRange(newItems, true);
            }
        }