AdvancedCollectionView and empty filter result

BitSmithy 1,751 Reputation points
2022-01-31T12:42:21.077+00:00

Hello,

I have problem with filtering based on AdvancedCollectionView from Windows Toolkit:

 XAML:
               <controls:DataGrid x:Name="dg" Grid.Row="1"
                           AutoGenerateColumns="True"
                           AutoGeneratingColumn="dg_AutoGeneratingColumn"
                           ></controls:DataGrid>


Code behind:
       ObservableCollection<string> strCol = new ObservableCollection<string>()
        public AdvancedCollectionView acv { get; set; }

            strCol.Add("aaa");
            strCol.Add("bbb");
            strCol.Add("ddd");
            strCol.Add("beeeb");
            strCol.Add("axxxaa");
            strCol.Add("bbb");

            acv = new AdvancedCollectionView(strCol);

            dg.ItemsSource = acv;

If I try to apply filter which should retur empty result, AdvancedCollectionView throws error:

    private void Filter_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
           acv.Filter = x => (((string)x).Contains("aaaaaa"));

    }

If I want to handle this error using try/catch, AdvancedCollectionView always shows last row of the filtered ObservableCollection, even if it doesnt feed filter rules:

    private void Filter_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        try
        {
            acv.Filter = x => (((string)x).Contains("a"));
            //acv.Refresh();
        }
        catch
        {


        }
    }

How to solve this problem, I want to have a filter which works well and doesnt throw exception (or this exception could be catch) even if User enters filter which returns empty collection view.

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Roy Li - MSFT 32,051 Reputation points Microsoft Vendor
    2022-02-01T05:59:37.667+00:00

    Hello,

    Welcome to Microsoft Q&A!

    The reason for this behavior is that when applying the filter, the AdvancedCollectionView will remove the items inside it and there must be at least one item in the collection, otherwise it will gives error - Index was out of range. To check this, you could take a look at the source code of the AdvancedCollectionView.

    Also, When you are using the try/catch block to handling the error, the actual value of AdvancedCollectionView is null. For example, you could get the ItemsSource of the DataGrid and check its count. The last row you saw is actually some cache of the DataGrid, not the real data.

    Currently, there is no way to avoid this when using the AdvancedCollectionView . You could report this on the WindowsCommunityToolkit Issues to see if the WindowsCommunityToolkit team could update this class.

    A possible way is that you could try to directly download and modify the source code of AdvancedCollectionView in the Github based on your request.

    And another way is to use ObservableCollection instead of AdvancedCollectionView. For your pervious question, you might need to create the column by yourself and set AutoGenerateColumns to false. So it won't trigger the AutoGenerateColumns event when you sort the data. This is also the same way how the official document implement sorting and flitering.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful