This is a simple trick to handle a common problem
Problem:
First see the below code, pay attention to comments
the line
Problem:
You provided a filter option for showing a list, you wanted user to add new items to list, and if list is filtered the new item should be added to the current filtered list.Requirements:
1. You used the ICollectionView to bind the list, if you don’t do.Solution:
2. On of these
- There is no item that doesn’t belong to any filter.
- You have a way to recognize newly created objects.
First see the below code, pay attention to comments
anICollectionView.Filter = o=> { var unit = o as Unit; if (unit.UnitCategory == null) { // This is a tricky code, there is 3 points: // 1. This code will be executed only if the list is shown in categorized mode // 2. All units have a unit category, so I didn't change any other unit's unit category // 3. Regarding to point 2 this unit which has no unit category is a new added unit, you bet! unit.UnitCategory = currentUnitCategoryOnFilter; // 4. So I can set the unit category of this newly created unit to the current filter } return unit.UnitCategory = currentUnitCategoryOnFilter); };
the line
if (unit.UnitCategory == null)is the way that I recognize this a new object because all units have a unit category.