Preserve the order after resetting the filter

15 views Asked by At

I use DynamicData for filtering purpose in DataGrid. I would like to preserve the order of my collection after resetting the filter. For example, I have some collection of strings: "var_1", "var_2", ..., "var_11", "var_12" . I want to get elements that contain '1'. After filtering I have: "var_1", "var_11", "var_12".

Filtering works fine.

After resetting the filter, I have: var_1", "var_11", "var_12", "var_2", ... I would like to see my collection in the initial order. How can do this?

My class with data:

class MyClass
{
   public string Name { get; set; }
   public string Type { get; set; }
}

Collection:

ReadOnlyObservableCollection<MyClass> _myCollection = new () {...}

How I filter:

Func<MyClass, bool> myFilter(string text) => my =>
{
    return string.IsNullOrEmpty(text) || my.Name.ToLower().Contains(text.ToLower());
}

var filterPredicate = this.WhenAnyValue(x => x.Name).Select(myFilter);

var _cleanUp = _sourceList.Connect().Filter(filterPredicate).Bind(out _myCollection).Subscribe();

I excpect the initial order.

0

There are 0 answers