WPF – Virtualizing Tree View – SelectedItem selection desappearing after item is selected through a Custom Search Box

I have a custom tree view user control that is using a Virtualizing Stack Panel since i have to display ~8000 items. The tree view also lives in a groupbox with custom SearchBox user control that allows users to search a tree view item and select it from the searchbox results.

The Search Box has a dependency property for the SelectedItem and the custom tree view also has a SelectedItem property that get set on the SelectionChanged event. in my view model i am binding a single SelectedItem property to both dependency properties stated above.

Since i am using a virtualizing stack panel and selecting items from the search box, I needed some additional logic to find the TreeViewItem that matched the selectedItem in the search box, for this I followed the following article: https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/how-to-find-a-treeviewitem-in-a-treeview?view=netframeworkdesktop-4.8. I made my own implementation so that it was faster.

Here is the code:

       private static TreeViewItem GetTreeViewItem(ItemsControl container, object item)
   {
       if (container == null) 
           return null;

       var rootFolder = (Folder)container.DataContext;
       var folderTreeItem = (IFolderTreeItem)item;

       var parentStack = new Stack<Folder>();
       var parentFolder = folderTreeItem.ParentFolder;
       while (parentFolder != rootFolder)
       {
           parentStack.Push(parentFolder);
           parentFolder = parentFolder.ParentFolder;
       }

       var rootTreeViewItem = (TreeViewItem)container;
       rootTreeViewItem.BringIntoView();
       rootTreeViewItem.IsExpanded = true;
       
       var virtualizingPanel = GetVirtualizingStackPanel(container);
       var currentFolder = rootFolder;
       while (parentStack.Count > 0)
       {
           var childFolder = parentStack.Pop();
           
           var childIndex = currentFolder.SubFolders.IndexOf(childFolder);
           
           if (childIndex == -1)
               throw new Exception();
           
           virtualizingPanel.BringIntoView(childIndex);
           var currentTreeViewItem = (TreeViewItem)rootTreeViewItem.ItemContainerGenerator.ContainerFromIndex(childIndex);
           currentTreeViewItem.IsExpanded = true;
           currentFolder = childFolder;
           rootTreeViewItem = currentTreeViewItem;
           virtualizingPanel = GetVirtualizingStackPanel(currentTreeViewItem);
       }
       
       var currentIndex = rootTreeViewItem.ItemContainerGenerator.Items.Cast<IFolderTreeItem>().ToList().FindIndex(tv => tv.Equals( folderTreeItem));
       
       virtualizingPanel.BringIntoView(currentIndex);
       var treeView = (TreeViewItem)rootTreeViewItem.ItemContainerGenerator.ContainerFromIndex(currentIndex);
       
       return treeView;
   }

Here is what my data model looks like:

    /// <summary>
/// Interface that propages path in order for a class to be displayed in a folder tree view
/// </summary>
internal interface IFolderTreeItem
{
    /// <summary>
    /// Gets the path folder tree where item located
    /// </summary>
    string FolderTreePath { get; }
    
    /// <summary>
    /// Gets or sets the folder where this item lives
    /// </summary>
    Folder ParentFolder { get; set;  }
    
    /// <summary>
    /// Gets the name of the object that will be displayed
    /// </summary>
    string Name { get; }
    
    /// <summary>
    /// Gets or sets the list of items to display on the tree view
    /// </summary>
    IEnumerable AllItems { get; }
}

Note: Folder being an instance of IFolderTreeItem
Note: Im using AllItems property to bind to the HierarchicalDataTemplate.
Note: GetVirtualizingStackPanel() and GetVisualChild() will match Microsoft article.
Note: Not using any behaviors or attached properties (only drag/drop library but that didnt seem to be causing the issue).

Everything that I have stated before is working fine, the issue that I have, is that for some items that were selected from the search box results, the TreeViewItem selection is appearing and disappearing right after. see video example here: https://imgur.com/a/eadwBGg (focus on the bottom of the video).

My suspicion is that it is related to virtualization but I could be wrong, I could provide more code if needed, please advice on anything that could help, i have been battling this for a couple of days and i am out of ideas. The weirdest part is that for some items it will work, for some items it wont, which its why i think its an issue with virtualization.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật