Search code examples
c#.net-coreavaloniaui

ICollectionView could not be found from Studio Code on .NET Core


I have a XAML and a MVVM showing a DataGrid with an ObservableCollection. All working and OK (more details in my previous, already answered by myself, question).

Now I'm trying to add a filter and I'd like to follow @mark-heath tutorial.

My project build throws the following

error CS0246: The type or namespace name 'ICollectionView' could not be found (are you missing a using directive or an assembly reference?) 

even though I did include the documented namespace.

using System.ComponentModel;

about which Studio Code shows Unnecessary using directive instead. My .csproj is on .NET Core 3.0

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>

The problem seems to be related with how I add the assembly reference.

dotnet add package WindowsBase

It restores the package using .NET Framework, but maybe this is wrong because I'm on .NET Core and on Linux.

Which is the correct way? Aside from the "you can't do that" answer... Maybe is there another, equivalent package to add... from Avalonia UI? Anyone knows or uses it?

Searching on Avalonia UI github and asking on gitter

I see there was an already closed github issue about that, so maybe is there a solution now? (I'm asking on Avalonia UI gitter channel too)


Solution

  • First of all - even in WPF and on Windows - the tutorial is wrong as per my comment there:

    you need to bind to the ICollectionView and not to the ObservableCollection to see the filtering effect.

    So, the view must be fixed as follows

    <DataGrid Items="{Binding PeopleView}" 
    

    Now, back to the question on Linux and in Avalonia UI.

    As suggested by Steven Kirk, I've looked at github DevTools source in avalonia, that does filtering in the view model, and this clue does the trick.

    So I declare PeopleView in the view model

    public DataGridCollectionView PeopleView { get; }
    

    as a DataGridCollectionView which is included in the needed namespace

    using Avalonia.Collections;
    

    and I can finally implement the filter

    public MainWindowViewModel()
    {
        People = new ObservableCollection<Person>(GenerateMockPeopleTable());
        PeopleView = new DataGridCollectionView(People);
        PeopleView.Filter = o => String.IsNullOrEmpty(Filter) ? true : ((Person)o).FirstName.Contains(Filter); 
    
    }