根据这篇文章提供的方法会导致搜索变慢及有时候搜索不到 WPF中ItemsControl应用虚拟化时找到子元素的方法,
具体可以修改为下面代码:
//Action action = () => //{ try { TreeViewItem itemSelected = null; //Force to generate every treeView item by using scroll item if (virtualizingPanel != null) { try { if (isCallEnd) { isCallEnd = false; virtualizingPanel.CallBringIndexIntoView(selectedIndex); isCallEnd = true; } } catch (System.Exception ex) { Logger.Error("CallBringIndexIntoView exception : ", ex); } itemSelected = (TreeViewItem)_currentSelectedItem.ItemContainerGenerator.ContainerFromIndex(selectedIndex); }
增加变量isCallEnd 用来防止循环调用,-》CallBringIndexIntoView-》ItemContainerGenerator_StatusChanged-》treeViewItem_BringIntoView-》CallBringIndexIntoView