{"record":{"id":"d756fe58aa40b8d0","repo":"stride3d/stride","slug":"bringitemtoview-cannot-be-used-when-the-tree-view-is","errorCode":null,"errorMessage":"BringItemToView cannot be used when the tree view is virtualizing.","messagePattern":"BringItemToView cannot be used when the tree view is virtualizing\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation.Wpf/Controls/TreeView.cs","lineNumber":158,"sourceCode":"        {\n            base.OnApplyTemplate();\n\n            scroller = DependencyObjectExtensions.CheckTemplatePart<ScrollViewer>(GetTemplateChild(ScrollViewerPartName));\n            if (scroller != null)\n            {\n                scroller.ScrollChanged += ScrollChanged;\n            }\n        }\n\n        // TODO: This method has been implemented with a lot of fail and retry, and should be cleaned.\n        // TODO: Also, it is probably close to work with virtualization, but it needs some testing\n        public bool BringItemToView([NotNull] object item, [NotNull] Func<object, object> getParent)\n        {\n            // Useful link: https://msdn.microsoft.com/en-us/library/ff407130%28v=vs.110%29.aspx\n            if (item == null) throw new ArgumentNullException(nameof(item));\n            if (getParent == null) throw new ArgumentNullException(nameof(getParent));\n            if (IsVirtualizing)\n                throw new InvalidOperationException(\"BringItemToView cannot be used when the tree view is virtualizing.\");\n\n            TreeViewItem container = null;\n\n            var path = new List<object> { item };\n            var parent = getParent(item);\n            while (parent != null)\n            {\n                path.Add(parent);\n                parent = getParent(parent);\n            }\n\n            for (var i = path.Count - 1; i >= 0; --i)\n            {\n                if (container != null)\n                    container = (TreeViewItem)container.ItemContainerGenerator.ContainerFromItem(path[i]);\n                else\n                    container = (TreeViewItem)ItemContainerGenerator.ContainerFromItem(path[i]);\n","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation.Wpf/Controls/TreeView.cs#L140-L176","documentation":"BringItemToView scrolls a TreeView so a given item is visible by walking parent containers to realize them. This only works with container generation that is not virtualized; when VirtualizingPanel.IsVirtualizing is true, containers for off-screen items do not exist and cannot be generated by this method, so the control throws InvalidOperationException up front instead of failing silently.","triggerScenarios":"Calling treeView.BringItemToView(item, getParent) on a TreeView whose virtualization is enabled (VirtualizingPanel.IsVirtualizing=true, e.g. inside a large list with VirtualizingStackPanel), passing non-null item and getParent.","commonSituations":"Programmatic navigation/selection restore in editor-like trees; code written for a non-virtualized tree later moved into a virtualized layout; enabling virtualization for performance on large trees and existing BringItemToView calls start throwing.","solutions":["Set VirtualizingPanel.IsVirtualizing=\"False\" on the TreeView (accepting the performance cost).","Remove/disable the BringItemToView call and instead use container-free navigation (e.g. expand path via the view-model then rely on selection scrolling).","Guard the call: check treeView.IsVirtualizing (or the attached property) before invoking and take an alternate path."],"exampleFix":"// before\ntreeView.BringItemToView(node, n => vm.GetParent(n));\n// after\nif (!treeView.IsVirtualizing)\n    treeView.BringItemToView(node, n => vm.GetParent(n));\nelse\n    ExpandPathTo(node); // expand ancestors so the container is realized","handlingStrategy":"validation","validationCode":"if (treeView.IsVirtualizing)\n    throw new NotSupportedException(\"BringItemToView requires a non-virtualizing TreeView\");\ntreeView.BringItemToView(item, parent => vm.GetParent(parent));","typeGuard":"bool CanBringToView(TreeView tv) => !tv.IsVirtualizing;","tryCatchPattern":"try { treeView.BringItemToView(item, getParent); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"virtualizing\")) { ExpandPathTo(item); /* fallback */ }","preventionTips":["Check IsVirtualizing before any programmatic scroll-to-item logic.","If virtualization is on, expand ancestors via the view-model instead of container walking.","Document the virtualization constraint next to any shared BringItemToView helper."],"tags":["wpf","treeview","virtualization"],"backgroundTag":"unsupported-operation","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}