lucasg/Dependencies · error · InvalidOperationException
The TargetConrol should use ICollectionView as ItemSource.
Error message
The TargetConrol should use ICollectionView as ItemSource.
What it means
InvalidOperationException thrown by FilterControl.ApplyFilterOnTarget when CollectionViewSource.GetDefaultView(TargetControl.Items.SourceCollection) returns null. FilterControl attaches its text filter as a predicate on an ICollectionView, so it requires the target's items source to resolve to a valid view. Note the message text has a typo ('TargetConrol', 'ItemSource') but the contract is: the TargetControl must be an ItemsControl whose SourceCollection yields a non-null default view.
Source
Thrown at DependenciesGui/FilterControl/FilterControl.cs:252
base.OnApplyTemplate();
AttachToVisualTree();
}
#endregion
#region Private Functions/Methods
private void ApplyFilterOnTarget()
{
if (TargetControl == null || TargetControl.Items.SourceCollection == null)
return;
ICollectionView collectionView = CollectionViewSource.GetDefaultView(TargetControl.Items.SourceCollection);
if (collectionView == null)
{
throw new InvalidOperationException("The TargetConrol should use ICollectionView as ItemSource.");
}
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);View on GitHub (pinned to 1997a40000)
Solutions
- Bind the TargetControl's ItemsSource to an ObservableCollection<T> / IList so GetDefaultView returns a ListCollectionView.
- Ensure data is loaded (SourceCollection non-null and enumerable) before the filter is applied.
- Set FilterTextBindingPath so the filter has a property to read (otherwise error 23 follows).
- Guard ApplyFilterOnTarget to no-op when collectionView == null instead of throwing.
Example fix
<!-- before -->
<filter:FilterControl TargetControl="{Binding ElementName=MyList}" />
<!-- after - bind ItemsSource and set the filter path -->
<filter:FilterControl TargetControl="{Binding ElementName=MyList}"
FilterTextBindingPath="Name" />
<ListBox x:Name="MyList" ItemsSource="{Binding Items}" /> 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 || string.IsNullOrEmpty(filterControl.FilterTextBindingPath)) return; // skip filtering Type guard
static bool HasFilterableView(ItemsControl target)
{
return target != null
&& target.Items.SourceCollection != null
&& CollectionViewSource.GetDefaultView(target.Items.SourceCollection) != null;
} Try / catch
try { filterControl.RaiseFilterEvent(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("ICollectionView"))
{ /* TargetControl misbound - log and disable filtering */ } Prevention
- Always bind TargetControl.ItemsSource to an ObservableCollection/IList in XAML.
- Always set FilterTextBindingPath alongside TargetControl.
- Load data before the FilterControl becomes visible/interactive.
When it happens
Trigger: ApplyFilterOnTarget runs (user types in the filter box and the dispatcher timer fires, or RaiseFilterEvent is called with no external handler) while TargetControl is set but its Items.SourceCollection does not produce a default ICollectionView.
Common situations: FilterControl.TargetControl is bound to a control whose ItemsSource is unbound or bound to null; bound to a custom non-IEnumerable/non-IList source; the control is shown before its data is loaded; XAML wires TargetControl but forgets ItemsSource.
Related errors
- The TargetControl 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/43d858ffad17f50e.
Report an issue: GitHub.