CollectionViewSource filtering logic in xaml

32 Views Asked by At

I have a CollectionViewSource defined in xaml:

<CollectionViewSource x:Key="DetailsViewSource1" Source="{Binding DetailsList}"
            xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="AA" Direction="Ascending" />
            <scm:SortDescription PropertyName="BB" Direction="Ascending" />
            <scm:SortDescription PropertyName="VV" Direction="Ascending" />
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>

And a listview using this collectionview as an ItemSource

 <ListView.ItemsSource>
                    <CompositeCollection>
                        <CollectionContainer Collection="{Binding Source={StaticResource DetailsViewSource1}}" />
                        <CollectionContainer Collection="{Binding Source={StaticResource DetailsViewSource2}}" />
                    </CompositeCollection>
                </ListView.ItemsSource>

And a Textbox where user types the search string. In the viewmodels I am maintaining two observable collections, one "DetailsList" to which CollectionViewSource is binded and other just called "actualList". Actually the idea is when user types the search string in the textbox, I change the DetailsList collection to a filtered collection, so that the list in the UI becomes the filtered one:

public string SeachDetailsInputString
    {
        get => _seachDetailsInputString;
        set
        {
            if (_seachDetailsInputString != value)
            {
                _seachDetailsInputString = value;

                if (!string.IsNullOrEmpty(_seachDetailsInputString))
                {
                    var filteredList = _actualList.Where(list => list.DisplayName.Contains(value) || list.Details.Contains(value));
                    DetailsList = new ObservableCollection<DetailsModel>(filteredList);
                }
                else
                {
                    DetailsList = _actualList;
                }
            }
        }
    }

and when user clears the textbox I assign the DetailsList to _actualList.

The issue is I have to maintain two observable collection for this filtering. I want to achieve this functionality using only one. I have also seen the filter event of the CollectionViewSource, but I am not sure if this could be used in my solution. Moreover, I have seen that CollectionViewSource is kept in the VM code following other solutions, I would like to keep the CollectionViewSource in the xaml.

0

There are 0 best solutions below