{"record":{"id":"38882b8ff0e6457f","repo":"dotnet/wpf","slug":"view-can-t-be-shared-by-more-than-one-listview","errorCode":null,"errorMessage":"View can't be shared by more than one ListView.","messagePattern":"View can't be shared by more than one ListView\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ListView.cs","lineNumber":93,"sourceCode":"        /// descriptor of the whole view. Include chrome/layout/item/...\n        /// </summary>\n        public ViewBase View\n        {\n            get { return (ViewBase)GetValue(ViewProperty); }\n            set { SetValue(ViewProperty, value); }\n        }\n\n        private static void OnViewChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)\n        {\n            ListView listView = (ListView)d;\n\n            ViewBase oldView = (ViewBase)e.OldValue;\n            ViewBase newView = (ViewBase)e.NewValue;\n            if (newView != null)\n            {\n                if (newView.IsUsed)\n                {\n                    throw new InvalidOperationException(SR.ListView_ViewCannotBeShared);\n                }\n                newView.IsUsed = true;\n            }\n\n            // In ApplyNewView ListView.ClearContainerForItemOverride will be called for each item.\n            // Should use old view to do clear item.\n            listView._previousView = oldView;\n            listView.ApplyNewView();\n            // After ApplyNewView, if item is removed, ListView.ClearContainerForItemOverride will be called.\n            // Then should use new view to do clear item.\n            listView._previousView = newView;\n\n            //Switch ViewAutomationPeer in ListViewAutomationPeer\n            ListViewAutomationPeer lvPeer = UIElementAutomationPeer.FromElement(listView) as ListViewAutomationPeer;\n            if (lvPeer != null)\n            {\n                lvPeer.ViewAutomationPeer?.ViewDetached();\n","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ListView.cs#L75-L111","documentation":"ListView throws this InvalidOperationException in its View property change callback (OnViewChanged) when the ViewBase instance being assigned is already in use by another ListView. Each ViewBase tracks an IsUsed flag because the ListView stores per-view item/container state on it; sharing one view instance across multiple ListViews would corrupt that state, so the assignment is rejected.","triggerScenarios":"Setting ListView.View (via code, XAML resource, style, or binding) to a ViewBase whose IsUsed is already true — e.g. assigning the same GridView instance to two ListView.View properties, or re-assigning a view still held by a previous ListView.","commonSituations":"Declaring one GridView as a shared XAML resource used by multiple ListViews; creating a single view object in code and applying it to several lists in a loop; moving a view between lists without first detaching it.","solutions":["Create a new ViewBase (e.g. new GridView()) for each ListView instead of sharing one instance.","If the view is declared in resources, add x:Shared=\"False\" so each lookup produces a fresh instance.","To move a view between lists, first set the old ListView's View to null and ensure IsUsed is reset before assigning.","Check view.IsUsed before assignment and create a fresh copy when it returns true."],"exampleFix":"// before: listView1.View = shared; listView2.View = shared; (throws InvalidOperationException) // after: listView1.View = new GridView { Columns = { new GridViewColumn { Header = \"Name\" } } }; listView2.View = new GridView { Columns = { new GridViewColumn { Header = \"Name\" } } };","handlingStrategy":"validation","validationCode":"static bool CanAssignView(ListView listView, ViewBase view) => view == null || ReferenceEquals(listView.View, view) || !view.IsUsed;","typeGuard":"static bool IsViewUnshared(ViewBase view) => view != null && !view.IsUsed;","tryCatchPattern":"try { listView.View = view; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"shared\")) { listView.View = CreateFreshView(); }","preventionTips":["Never put a single GridView/ViewBase in a shared XAML resource without x:Shared=\"False\".","Create a new view instance per ListView via a factory method.","Re-check IsUsed before re-assigning a view that previously belonged to another ListView."],"tags":["wpf","listview","viewbase","object-sharing","invalid-operation"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}