@@ -241,6 +241,47 @@ public BranchTree()
241241 InitializeComponent ( ) ;
242242 }
243243
244+ public void Select ( Models . Branch branch )
245+ {
246+ if ( branch == null )
247+ return ;
248+
249+ _disableSelectionChangingEvent = true ;
250+
251+ var treePath = new List < ViewModels . BranchTreeNode > ( ) ;
252+ FindTreePath ( treePath , Nodes , branch . Name , 0 ) ;
253+
254+ if ( treePath . Count == 0 )
255+ {
256+ _disableSelectionChangingEvent = false ;
257+ return ;
258+ }
259+
260+ var oldRowCount = Rows . Count ;
261+ var rows = Rows ;
262+ for ( var i = 0 ; i < treePath . Count - 1 ; i ++ )
263+ {
264+ var node = treePath [ i ] ;
265+ if ( ! node . IsExpanded )
266+ {
267+ node . IsExpanded = true ;
268+
269+ var idx = rows . IndexOf ( node ) ;
270+ var subtree = new List < ViewModels . BranchTreeNode > ( ) ;
271+ MakeRows ( subtree , node . Children , node . Depth + 1 ) ;
272+ rows . InsertRange ( idx + 1 , subtree ) ;
273+ }
274+ }
275+
276+ var target = treePath [ treePath . Count - 1 ] ;
277+ BranchesPresenter . SelectedItem = target ;
278+ BranchesPresenter . ScrollIntoView ( target ) ;
279+ _disableSelectionChangingEvent = false ;
280+
281+ if ( oldRowCount != rows . Count )
282+ RaiseEvent ( new RoutedEventArgs ( RowsChangedEvent ) ) ;
283+ }
284+
244285 public void UnselectAll ( )
245286 {
246287 BranchesPresenter . SelectedItem = null ;
@@ -534,6 +575,23 @@ private void CollectBranchesInNode(List<Models.Branch> outs, ViewModels.BranchTr
534575 CollectBranchesInNode ( outs , sub ) ;
535576 }
536577
578+ private void FindTreePath ( List < ViewModels . BranchTreeNode > outPath , List < ViewModels . BranchTreeNode > collection , string path , int start )
579+ {
580+ if ( start >= path . Length - 1 )
581+ return ;
582+
583+ var sepIdx = path . IndexOf ( '/' , start ) ;
584+ var name = sepIdx < 0 ? path . Substring ( start ) : path . Substring ( start , sepIdx - start ) ;
585+ foreach ( var node in collection )
586+ {
587+ if ( node . Name . Equals ( name , StringComparison . Ordinal ) )
588+ {
589+ outPath . Add ( node ) ;
590+ FindTreePath ( outPath , node . Children , path , sepIdx + 1 ) ;
591+ }
592+ }
593+ }
594+
537595 private bool _disableSelectionChangingEvent = false ;
538596 }
539597}
0 commit comments