lucasg/Dependencies · error · InvalidOperationException
The TargetControl should use ICollectionView as ItemSource.
Error message
The TargetControl should use ICollectionView as ItemSource.
What it means
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').
Source
Thrown at DependenciesGui/FilterControl/FilterControl.cs:274
if (string.IsNullOrEmpty(this.FilterTextBindingPath))
{
throw new InvalidOperationException("FilterTextBindingPath is not set.");
}
collectionView.Filter = (m => (GetDataValue<string>(m, this.FilterTextBindingPath).IndexOf(this.FilterText, StringComparison.InvariantCultureIgnoreCase) > -1));
}
private void ClearFilterOnTarget()
{
if (TargetControl == null || TargetControl.Items.SourceCollection == null)
return;
ICollectionView collectionView = CollectionViewSource.GetDefaultView(TargetControl.Items.SourceCollection);
if (collectionView == null)
{
throw new InvalidOperationException("The TargetControl should use ICollectionView as ItemSource.");
}
collectionView.Filter = null;
this.Visibility = System.Windows.Visibility.Collapsed;
}
public void RaiseFilterEvent()
{
FilterEventArgs args = new FilterEventArgs(FilterEvent, this, this.FilterText);
RaiseEvent(args);
if (!args.IsFilterApplied)
{
ApplyFilterOnTarget();
}
}
private T GetDataValue<T>(object data, string propertyName)View on GitHub (pinned to 1997a40000)
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.
Defensive patterns
Strategy: validation
Validate before calling
if (filterControl.TargetControl == null
|| filterControl.TargetControl.Items.SourceCollection == null) return;
ICollectionView view = CollectionViewSource.GetDefaultView(filterControl.TargetControl.Items.SourceCollection);
if (view == null) return; // cannot clear a filter on a missing view Type guard
static bool HasFilterableView(ItemsControl target)
{
return target != null
&& target.Items.SourceCollection != null
&& CollectionViewSource.GetDefaultView(target.Items.SourceCollection) != null;
} Try / catch
try { filterControl.Clear(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("ICollectionView"))
{ /* TargetControl misbound - ignore clear */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The TargetConrol should use ICollectionView as ItemSource.
- FilterTextBindingPath is not set.
- Can only convert to string.
- Can only convert an instance of enum.
- peview.exe file could not be found !
AI-assisted analysis of lucasg/Dependencies@1997a40000 (2026-08-13).
Data as JSON: /api/errors/65758efae6a92c61.
Report an issue: GitHub.