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

  1. Bind TargetControl's ItemsSource to an enumerable collection (ObservableCollection<T>/IList).
  2. Make ClearFilterOnTarget tolerate a null view (no-op) instead of throwing.
  3. Ensure the data is loaded before the FilterControl is interactable.
  4. 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

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


AI-assisted analysis of lucasg/Dependencies@1997a40000 (2026-08-13). Data as JSON: /api/errors/65758efae6a92c61. Report an issue: GitHub.