{"record":{"id":"79a8fa292e04654c","repo":"dotnet/wpf","slug":"sr-defaultvaluemaynotbeexpression","errorCode":null,"errorMessage":"SR.DefaultValueMayNotBeExpression","messagePattern":"SR\\.DefaultValueMayNotBeExpression","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs","lineNumber":393,"sourceCode":"\n        private static void ValidateDefaultValueCommon(\n            object defaultValue,\n            Type propertyType,\n            string propertyName,\n            ValidateValueCallback validateValueCallback,\n            bool checkThreadAffinity)\n        {\n            // Ensure default value is the correct type\n            if (!IsValidType(defaultValue, propertyType))\n            {\n                throw new ArgumentException(SR.Format(SR.DefaultValuePropertyTypeMismatch, propertyName));\n            }\n\n            // An Expression used as default value won't behave as expected since\n            //  it doesn't get evaluated.  We explicitly fail it here.\n            if (defaultValue is Expression )\n            {\n                throw new ArgumentException(SR.DefaultValueMayNotBeExpression);\n            }\n\n            if (checkThreadAffinity)\n            {\n                // If the default value is a DispatcherObject with thread affinity\n                // we cannot accept it as a default value. If it implements ISealable\n                // we attempt to seal it; if not we throw  an exception. Types not\n                // deriving from DispatcherObject are allowed - it is up to the user to\n                // make any custom types free-threaded.\n\n\n                if (defaultValue is DispatcherObject dispatcherObject && dispatcherObject.Dispatcher != null)\n                {\n                    // Try to make the DispatcherObject free-threaded if it's an\n                    // ISealable.\n\n\n                    if (dispatcherObject is ISealable valueAsISealable && valueAsISealable.CanSeal)","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs#L375-L411","documentation":"An Expression instance cannot be used as a dependency property default value because defaults are copied/stored raw and never evaluated, producing incorrect behavior. WPF explicitly rejects Expression defaults during metadata validation.","triggerScenarios":"new PropertyMetadata(BindingOperations.SomeExpression) or passing any Expression-derived object as the defaultValue of Register/OverrideMetadata/AddOwner metadata.","commonSituations":"Developers trying to make a property's default a Binding or dynamic expression instead of setting the binding in XAML/code after construction.","solutions":["Use a plain value (or null) as the default.","Apply bindings/animations after element construction (e.g. in XAML or via SetBinding) rather than as a default.","If deferred value computation is needed, use a value converter or CoerceValueCallback instead."],"exampleFix":"// before\nnew PropertyMetadata(new Binding(\"ViewModelValue\"))\n// after\nnew PropertyMetadata(null) // bind in XAML or via SetBinding at runtime","handlingStrategy":"validation","validationCode":"if (defaultValue is System.Windows.Expression) throw new ArgumentException(\"Default may not be an Expression\");","typeGuard":"static bool IsNotExpression(object v) => v is not System.Windows.Expression;","tryCatchPattern":null,"preventionTips":["Never pass Binding/Expression objects as defaults; bind after construction","Use null default plus SetBinding in XAML or code"],"tags":["wpf","dependency-property","expression"],"backgroundTag":"invalid-argument-value","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"}