{"record":{"id":"8ee3141ede0c73a6","repo":"dotnet/wpf","slug":"sr-format-sr-propertyisinitializeonly-datatype-this-gettype","errorCode":null,"errorMessage":"SR.Format(SR.PropertyIsInitializeOnly, \"DataType\", this.GetType().Name)","messagePattern":"SR\\.Format\\(SR\\.PropertyIsInitializeOnly, \"DataType\", this\\.GetType\\(\\)\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateKey.cs","lineNumber":69,"sourceCode":"            }\n\n            _initializing = false;\n        }\n\n#endregion ISupportInitialize\n\n        /// <summary>\n        /// The type for which the template is designed.  This is either\n        /// a Type (for object data), or a string (for XML data).  In the latter\n        /// case the string denotes the XML tag name.\n        /// </summary>\n        public object DataType\n        {\n            get { return _dataType; }\n            set\n            {\n                if (!_initializing)\n                    throw new InvalidOperationException(SR.Format(SR.PropertyIsInitializeOnly, \"DataType\", this.GetType().Name));\n                if (_dataType != null && value != _dataType)\n                    throw new InvalidOperationException(SR.Format(SR.PropertyIsImmutable, \"DataType\", this.GetType().Name));\n\n                Exception ex = ValidateDataType(value, \"value\");\n                if (ex != null)\n                    throw ex;\n\n                _dataType = value;\n            }\n        }\n\n        /// <summary> Override of Object.GetHashCode() </summary>\n        public override int GetHashCode()\n        {\n            // note that the hash code can change, but only during intialization\n            // and only once (DataType can only be changed once, from null to\n            // non-null, and that can only happen during [Begin/End]Init).\n            // Technically this is still a violation of the \"constant during","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateKey.cs#L51-L87","documentation":"The DataType property of TemplateKey is write-once and only during the initialization window. If a setter is invoked outside of the BeginInit/EndInit pair, or DataType is set to a different value after it was already assigned, the property setter throws InvalidOperationException. This guarantees template keys are immutable once committed.","triggerScenarios":"Setting TemplateKey.DataType directly without calling BeginInit() first; setting DataType again (to a different value) after it was already assigned even inside an initialization block.","commonSituations":"Developer treats DataType like a normal mutable property; tooling that reuses and reconfigures a TemplateKey instance for a second template; data-binding attempts that try to assign DataType after creation.","solutions":["Call BeginInit() before assigning DataType and EndInit() afterwards.","Create a new TemplateKey instance instead of mutating DataType on an existing one.","If a different type is needed, construct a fresh key with the desired DataType in the constructor-supported flow."],"exampleFix":"// before\nvar key = new TemplateKey();\nkey.DataType = typeof(Customer); // throws: not initializing\n// after\nvar key = new TemplateKey();\nkey.BeginInit();\nkey.DataType = typeof(Customer);\nkey.EndInit();","handlingStrategy":"validation","validationCode":"if (key.DataType != null)\n    throw new InvalidOperationException(\"TemplateKey.DataType already set; create a new key.\");","typeGuard":null,"tryCatchPattern":"try { key.DataType = value; }\ncatch (InvalidOperationException) { key = CreateNewKey(value); }","preventionTips":["Treat TemplateKey as immutable after EndInit.","Always wrap DataType assignment in BeginInit/EndInit.","Never reuse key instances across different types."],"tags":["wpf","initialization","immutable-state"],"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"}