@@ -214,6 +214,13 @@ public int ScrollOffsetHorizontal {
214
214
215
215
CursorVisibility desiredCursorVisibility = CursorVisibility . Invisible ;
216
216
217
+ /// <summary>
218
+ /// Interface for filtering which lines of the tree are displayed
219
+ /// e.g. to provide text searching. Defaults to <see langword="null"/>
220
+ /// (no filtering).
221
+ /// </summary>
222
+ public ITreeViewFilter < T > Filter = null ;
223
+
217
224
/// <summary>
218
225
/// Get / Set the wished cursor when the tree is focused.
219
226
/// Only applies when <see cref="MultiSelect"/> is true.
@@ -541,7 +548,12 @@ private IReadOnlyCollection<Branch<T>> BuildLineMap ()
541
548
List < Branch < T > > toReturn = new List < Branch < T > > ( ) ;
542
549
543
550
foreach ( var root in roots . Values ) {
544
- toReturn . AddRange ( AddToLineMap ( root ) ) ;
551
+
552
+ var toAdd = AddToLineMap ( root , false , out var isMatch ) ;
553
+ if ( isMatch )
554
+ {
555
+ toReturn . AddRange ( toAdd ) ;
556
+ }
545
557
}
546
558
547
559
cachedLineMap = new ReadOnlyCollection < Branch < T > > ( toReturn ) ;
@@ -551,17 +563,44 @@ private IReadOnlyCollection<Branch<T>> BuildLineMap ()
551
563
return cachedLineMap ;
552
564
}
553
565
554
- private IEnumerable < Branch < T > > AddToLineMap ( Branch < T > currentBranch )
566
+ private bool IsFilterMatch ( Branch < T > branch )
567
+ {
568
+ return Filter ? . IsMatch ( branch . Model ) ?? true ;
569
+ }
570
+
571
+ private IEnumerable < Branch < T > > AddToLineMap ( Branch < T > currentBranch , bool parentMatches , out bool match )
555
572
{
556
- yield return currentBranch ;
573
+ bool weMatch = IsFilterMatch ( currentBranch ) ;
574
+ bool anyChildMatches = false ;
575
+
576
+ var toReturn = new List < Branch < T > > ( ) ;
577
+ var children = new List < Branch < T > > ( ) ;
557
578
558
579
if ( currentBranch . IsExpanded ) {
559
580
foreach ( var subBranch in currentBranch . ChildBranches . Values ) {
560
- foreach ( var sub in AddToLineMap ( subBranch ) ) {
561
- yield return sub ;
581
+
582
+ foreach ( var sub in AddToLineMap ( subBranch , weMatch , out var childMatch ) ) {
583
+
584
+ if ( childMatch )
585
+ {
586
+ children . Add ( sub ) ;
587
+ anyChildMatches = true ;
588
+ }
562
589
}
563
590
}
564
591
}
592
+
593
+ if ( parentMatches || weMatch || anyChildMatches )
594
+ {
595
+ match = true ;
596
+ toReturn . Add ( currentBranch ) ;
597
+ }
598
+ else {
599
+ match = false ;
600
+ }
601
+
602
+ toReturn . AddRange ( children ) ;
603
+ return toReturn ;
565
604
}
566
605
567
606
/// <summary>
@@ -1289,9 +1328,9 @@ protected void CollapseImpl (T toCollapse, bool all)
1289
1328
}
1290
1329
1291
1330
/// <summary>
1292
- /// Clears any cached results of <see cref="BuildLineMap"/>
1331
+ /// Clears any cached results of the tree state.
1293
1332
/// </summary>
1294
- protected void InvalidateLineMap ( )
1333
+ public void InvalidateLineMap ( )
1295
1334
{
1296
1335
cachedLineMap = null ;
1297
1336
}
0 commit comments