{"record":{"id":"dba149e38b6dc508","repo":"dotnet/wpf","slug":"sr-relativesourcemodeisimmutable","errorCode":null,"errorMessage":"SR.RelativeSourceModeIsImmutable","messagePattern":"SR\\.RelativeSourceModeIsImmutable","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/RelativeSource.cs","lineNumber":154,"sourceCode":"        /// <summary>mode of RelativeSource\n        /// </summary>\n        /// <remarks> Mode is read-only after initialization.\n        /// If Mode is not set explicitly, setting AncestorType or AncestorLevel will implicitly lock the Mode to FindAncestor. </remarks>\n        /// <exception cref=\"InvalidOperationException\"> RelativeSource Mode is immutable after initialization;\n        /// instead of changing the Mode on this instance, create a new RelativeSource or use a different static instance. </exception>\n        [ConstructorArgument(\"mode\")]\n        public RelativeSourceMode Mode\n        {\n            get { return _mode; }\n            set\n            {\n                if (IsUninitialized)\n                {\n                    InitializeMode(value);\n                }\n                else if (value != _mode)    // mode changes are not allowed\n                {\n                    throw new InvalidOperationException(SR.RelativeSourceModeIsImmutable);\n                }\n            }\n        }\n\n        /// <summary> The Type of ancestor to look for, in FindAncestor mode.\n        /// </summary>\n        /// <remarks> if Mode has not been set explicitly, setting AncestorType will implicitly lock Mode to FindAncestor. </remarks>\n        /// <exception cref=\"InvalidOperationException\"> RelativeSource is not in FindAncestor mode </exception>\n        public Type AncestorType\n        {\n            get { return _ancestorType; }\n            set\n            {\n                if (IsUninitialized)\n                {\n                    Debug.Assert(_mode == RelativeSourceMode.FindAncestor);\n                    AncestorLevel = 1;  // lock the mode and set default level\n                }","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/RelativeSource.cs#L136-L172","documentation":"RelativeSource.Mode is immutable once the object has been initialized. Attempting to change Mode to a different value after initialization throws InvalidOperationException; only setting it while still uninitialized (IsUninitialized) is allowed.","triggerScenarios":"Setting RelativeSource.Mode after EndInit/first use with a value different from the current _mode; reusing one RelativeSource instance across bindings with different modes.","commonSituations":"Recycling a shared RelativeSource between bindings; mutating a Mode declared in XAML from code-behind after load; data-trigger/style code that rewrites the mode at runtime.","solutions":["Create a new RelativeSource instance with the desired mode instead of mutating","Set Mode only before the source is used/initialized","Freeze/share per-mode RelativeSource instances (they are intended to be immutable after setup)"],"exampleFix":"// before\nrs.Mode = RelativeSourceMode.Self; // throws if rs initialized as FindAncestor\n// after\nvar newRs = new RelativeSource(RelativeSourceMode.Self);","handlingStrategy":"validation","validationCode":"// decide the mode before first use; never rewrite it later\nvar rs = new RelativeSource(desiredMode);","typeGuard":null,"tryCatchPattern":"try { rs.Mode = newMode; }\ncatch (InvalidOperationException) { rs = new RelativeSource(newMode); }","preventionTips":["Treat RelativeSource as immutable after initialization","Create a new instance instead of mutating Mode","Don't share one RelativeSource across different-mode bindings"],"tags":["wpf","data-binding","relativesource","immutability"],"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"}