У меня есть ObservableCollection<Recipe>
, который связан с ICollectionView
. Можно фильтровать по моей личной реализации предиката. ICollectionView
связан с dataGrid
.
Теперь я хотел бы изменить порядок списка Recipes
после фильтрации коллекции.
Рецепт получил свойство «Идентификатор». Если вы ввели точно идентификатор рецепта, его следует поместить в начало списка.
Пример:
Если вы введете 1234
в фильтр, Recipe
с Identifier = 1234
следует поместить в начало списка вместо того, чтобы занимать второе место.
public ICollectionView RecipeCollection
{
get => _RecipeCollection;
set
{
_RecipeCollection = value;
OnPropertyChanged();
}
}
private ICollectionView _RecipeCollection;
Recipes = new ObservableCollection<Recipe>(databaseQuery.Result);
RecipeCollection = CollectionViewSource.GetDefaultView(Recipes);
RecipeCollection.Filter = CollectionViewSource_Filter;
private bool CollectionViewSource_Filter(object item)
{
if (item is Recipe recipe)
{
if (string.IsNullOrEmpty(SearchBox.Text))
{
return true;
}
string filter = SearchBox.Text.ToLower();
if (recipe.Identifier.ToLower().Contains(filter))
{
return true;
}
if (!string.IsNullOrEmpty(recipe.Name) && recipe.Name.ToLower().Contains(filter))
{
return true;
}
if (!string.IsNullOrEmpty(recipe.Description) && recipe.Description.ToLower().Contains(filter))
{
return true;
}
}
return false;
}