{"record":{"id":"d955d82f30c171a8","repo":"dotnet/wpf","slug":"invalidoperationexception-dependencypropertykey","errorCode":null,"errorMessage":"InvalidOperationException()","messagePattern":"InvalidOperationException\\(\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyPropertyKey.cs","lineNumber":44,"sourceCode":"            {\n                return _dp;\n            }\n        }\n\n        internal DependencyPropertyKey(DependencyProperty dp)\n        {\n            _dp = dp;\n        }\n\n        /// <summary>\n        ///     Override the metadata of a property that is already secured with\n        /// this key.\n        /// </summary>\n        public void OverrideMetadata( Type forType, PropertyMetadata typeMetadata )\n        {\n            if( _dp == null )\n            {\n                throw new InvalidOperationException();\n            }\n\n            _dp.OverrideMetadata( forType, typeMetadata, this );\n        }\n\n        // This is not a property setter because we can't have a public\n        //  property getter and a internal property setter on the same property.\n        internal void SetDependencyProperty(DependencyProperty dp)\n        {\n            Debug.Assert(_dp==null,\"This should only be used when we need a placeholder and have a temporary value of null. It should not be used to change this property.\");\n            _dp = dp;\n        }\n\n        private DependencyProperty _dp = null;\n    }\n}\n\n","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyPropertyKey.cs#L26-L62","documentation":"DependencyPropertyKey.OverrideMetadata throws a bare InvalidOperationException when the key's _dp field is null — the key was never actually bound to a dependency property, i.e. it was not produced (or is no longer backed) by a DependencyPropertyKey registration. Calling OverrideMetadata on such an unbound key cannot proceed.","triggerScenarios":"Constructing a DependencyPropertyKey via its (non-public) constructor or getting a default-initialized instance and calling OverrideMetadata on it; deserialization or reflection paths that yield a key without _dp set; misuse in unit tests (see the listed test names exercising null/invalid forType and metadata cases).","commonSituations":"Keys stored in fields that were never assigned the RegisterReadOnly result; serialization round-trips dropping the internal _dp link; test scaffolding creating throwaway keys.","solutions":["Ensure the key comes from DependencyProperty.RegisterReadOnly (or RegisterAttachedReadOnly) and that the returned key is the one used","Check that the static key field is initialized before OverrideMetadata is called (static-field initialization order in the declaring type)","If _dp can be null in your scenario, guard: only call key.OverrideMetadata after verifying key.DependencyProperty != null"],"exampleFix":"// before\nprivate static DependencyPropertyKey MyPropKey; // never assigned\nMyPropKey.OverrideMetadata(typeof(C), meta); // _dp == null -> throws\n// after\nprivate static readonly DependencyPropertyKey MyPropKey =\n    DependencyProperty.RegisterReadOnly(nameof(MyProp), typeof(object), typeof(Owner), new PropertyMetadata());\nMyPropKey.OverrideMetadata(typeof(C), meta); // key is bound to a real DP","handlingStrategy":"type-guard","validationCode":"if (key == null || key.DependencyProperty == null)\n    throw new InvalidOperationException(\"DependencyPropertyKey is not bound to a DP\");","typeGuard":"static bool IsUsableKey(DependencyPropertyKey key) => key?.DependencyProperty != null;","tryCatchPattern":"try { key.OverrideMetadata(forType, meta); }\ncatch (InvalidOperationException) { /* unbound key: re-create via RegisterReadOnly */ }","preventionTips":["Assign DependencyPropertyKey fields directly from RegisterReadOnly (never leave null/default)","Make key fields static readonly to guarantee initialization order","Guard key usage with key.DependencyProperty != null in diagnostic code"],"tags":["wpf","dependency-property","dependencypropertykey","uninitialized","invalid-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-22T01:17:13.364Z"}