{"record":{"id":"65758efae6a92c61","repo":"lucasg/Dependencies","slug":"the-targetcontrol-should-use-icollectionview-as-it","errorCode":null,"errorMessage":"The TargetControl should use ICollectionView as ItemSource.","messagePattern":"The TargetControl should use ICollectionView as ItemSource\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DependenciesGui/FilterControl/FilterControl.cs","lineNumber":274,"sourceCode":"            if (string.IsNullOrEmpty(this.FilterTextBindingPath))\n            {\n                throw new InvalidOperationException(\"FilterTextBindingPath is not set.\");\n            }\n\n            collectionView.Filter = (m => (GetDataValue<string>(m, this.FilterTextBindingPath).IndexOf(this.FilterText, StringComparison.InvariantCultureIgnoreCase) > -1));\n\n        }\n\n        private void ClearFilterOnTarget()\n        {\n            if (TargetControl == null || TargetControl.Items.SourceCollection == null)\n                return;\n\n            ICollectionView collectionView = CollectionViewSource.GetDefaultView(TargetControl.Items.SourceCollection);\n\n            if (collectionView == null)\n            {\n                throw new InvalidOperationException(\"The TargetControl should use ICollectionView as ItemSource.\");\n            }            \n\n            collectionView.Filter = null;\n            this.Visibility = System.Windows.Visibility.Collapsed;\n        }\n\n        public void RaiseFilterEvent()\n        {\n            FilterEventArgs args = new FilterEventArgs(FilterEvent, this, this.FilterText);\n            RaiseEvent(args);\n\n            if (!args.IsFilterApplied)\n            {\n                ApplyFilterOnTarget();\n            }\n        }\n\n        private T GetDataValue<T>(object data, string propertyName)","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/lucasg/Dependencies/blob/1997a40000b77bd3326cbc33672a7b9f78bb23f3/DependenciesGui/FilterControl/FilterControl.cs#L256-L292","documentation":"InvalidOperationException thrown by FilterControl.ClearFilterOnTarget (the clear/reset path) when CollectionViewSource.GetDefaultView(TargetControl.Items.SourceCollection) returns null. Clearing the filter sets collectionView.Filter = null and collapses the control, so it still needs a valid ICollectionView. This is the clear-path twin of error 22 with corrected spelling ('TargetControl').","triggerScenarios":"ClearFilterOnTarget runs (user presses Escape or clicks the clear button, which calls Clear()) while TargetControl is set but its Items.SourceCollection does not resolve to a default ICollectionView.","commonSituations":"Same misconfiguration as error 22 but surfaced when clearing rather than applying: ItemsSource unbound, bound to null, or to a non-enumerable source; data unloaded when the user clears the filter.","solutions":["Bind TargetControl's ItemsSource to an enumerable collection (ObservableCollection<T>/IList).","Make ClearFilterOnTarget tolerate a null view (no-op) instead of throwing.","Ensure the data is loaded before the FilterControl is interactable.","Reproduce error 22's fix first - once the view exists, both apply and clear paths work."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if (filterControl.TargetControl == null\n    || filterControl.TargetControl.Items.SourceCollection == null) return;\nICollectionView view = CollectionViewSource.GetDefaultView(filterControl.TargetControl.Items.SourceCollection);\nif (view == null) return; // cannot clear a filter on a missing view","typeGuard":"static bool HasFilterableView(ItemsControl target)\n{\n    return target != null\n        && target.Items.SourceCollection != null\n        && CollectionViewSource.GetDefaultView(target.Items.SourceCollection) != null;\n}","tryCatchPattern":"try { filterControl.Clear(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"ICollectionView\"))\n{ /* TargetControl misbound - ignore clear */ }","preventionTips":["Fix the TargetControl binding once (see error 22) - both apply and clear paths then work.","Make ClearFilterOnTarget tolerate a null view by no-op'ing.","Disable the clear button/Escape handling until a valid view exists."],"tags":["wpf","filtercontrol","binding","icollectionview","invalidoperation"],"backgroundTag":null,"analyzedSha":"1997a40000b77bd3326cbc33672a7b9f78bb23f3","analyzedAt":"2026-08-13T18:14:41.696Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}