@@ -127,8 +127,6 @@ public object this[int index]
127127
128128 public bool HasChildElements => ( this . filtered . Count > 0 ) ;
129129
130- public bool IsFiltering => Options ? . Filter != null ;
131-
132130 public bool Contains ( object value )
133131 {
134132 return IndexOf ( value ) != - 1 ;
@@ -289,15 +287,19 @@ private string GetKey (object element)
289287
290288 private bool MatchesFilter ( Element element )
291289 {
292- if ( ! IsFiltering )
290+ if ( ! GetIsFiltering ( ) )
291+ return true ;
292+ if ( element . ChildrenView != null && ! element . ChildrenView . HasChildElements )
293+ return false ;
294+ if ( Options . Filter == null )
293295 return true ;
294296
295297 return Options . Filter ( element . Item ) ;
296298 }
297299
298300 private void FilterCore ( bool notify = true , bool isPureSubset = false )
299301 {
300- if ( ! IsFiltering ) {
302+ if ( ! GetIsFiltering ( ) ) {
301303 if ( this . arranged . Count == this . filtered . Count )
302304 return ;
303305 }
@@ -312,8 +314,7 @@ private void FilterCore (bool notify = true, bool isPureSubset = false)
312314
313315 var toRemove = new List < string > ( ) ;
314316 foreach ( var kvp in this . filtered ) {
315- var childView = kvp . Value . ChildrenView ;
316- if ( ( childView != null && ! childView . HasChildElements ) || ! MatchesFilter ( kvp . Value ) ) {
317+ if ( ! MatchesFilter ( kvp . Value ) ) {
317318 toRemove . Add ( kvp . Key ) ;
318319 }
319320 }
@@ -324,7 +325,7 @@ private void FilterCore (bool notify = true, bool isPureSubset = false)
324325 var toAdd = new List < string > ( filteredOut . Count ) ;
325326 foreach ( string key in filteredOut ) {
326327 Element e = this . arranged [ key ] ;
327- if ( ! IsFiltering || Options . Filter ( e . Item ) )
328+ if ( MatchesFilter ( e ) )
328329 toAdd . Add ( key ) ;
329330 }
330331 toAdd . Sort ( Comparer ) ;
@@ -435,8 +436,6 @@ private void Reset (bool notify = true)
435436 this . arranged . Clear ( ) ;
436437 this . filtered . Clear ( ) ;
437438
438- bool filtering = IsFiltering ;
439-
440439 foreach ( var sourceItem in this . source . Cast < object > ( ) . Select ( o => new { Item = o , Key = GetKey ( o ) } ) . OrderBy ( e => e . Key , Comparer ) ) {
441440 Element e = new Element {
442441 Item = sourceItem . Item
@@ -453,7 +452,7 @@ private void Reset (bool notify = true)
453452 e . ChildrenView = new SimpleCollectionView ( children , Options . ChildOptions , this , sourceItem . Key , sourceItem . Item ) ;
454453 }
455454
456- if ( ! filtering || this . options . Filter ( e . Item ) )
455+ if ( MatchesFilter ( e ) )
457456 this . filtered . Add ( sourceItem . Key , e ) ;
458457 }
459458
@@ -499,5 +498,27 @@ private void OnCollectionChanged (NotifyCollectionChangedEventArgs e)
499498 }
500499 }
501500 }
501+
502+ private bool GetIsFiltering ( )
503+ {
504+ // Generally our hierarchy is at max 3 levels deep, this can be cached if we find it problematic
505+ SimpleCollectionView parent = this ;
506+ while ( parent != null ) {
507+ if ( parent . Options ? . Filter != null )
508+ return true ;
509+
510+ parent = parent . parent ;
511+ }
512+
513+ SimpleCollectionViewOptions childOptions = Options . ChildOptions ;
514+ while ( childOptions != null ) {
515+ if ( childOptions . Filter != null )
516+ return true ;
517+
518+ childOptions = childOptions . ChildOptions ;
519+ }
520+
521+ return false ;
522+ }
502523 }
503524}
0 commit comments