{"record":{"id":"7bcd7783974d2410","repo":"AvaloniaUI/Avalonia","slug":"cannot-add-value-to-donothing-binding-value","errorCode":null,"errorMessage":"Cannot add value to DoNothing binding value.","messagePattern":"Cannot add value to DoNothing binding value\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/BindingValue.cs","lineNumber":183,"sourceCode":"                    new BindingNotification(Error!, BindingErrorType.DataValidationError, Value),\n                _ => throw new NotSupportedException(\"Invalid BindingValueType.\"),\n            };\n        }\n\n        /// <summary>\n        /// Returns a new binding value with the specified value.\n        /// </summary>\n        /// <param name=\"value\">The new value.</param>\n        /// <returns>The new binding value.</returns>\n        /// <exception cref=\"InvalidOperationException\">\n        /// The binding type is <see cref=\"BindingValueType.UnsetValue\"/> or\n        /// <see cref=\"BindingValueType.DoNothing\"/>.\n        /// </exception>\n        public BindingValue<T> WithValue(T value)\n        {\n            if (Type == BindingValueType.DoNothing)\n            {\n                throw new InvalidOperationException(\"Cannot add value to DoNothing binding value.\");\n            }\n\n            var type = Type == BindingValueType.UnsetValue ? BindingValueType.Value : Type;\n            return new BindingValue<T>(type | BindingValueType.HasValue, value, Error);\n        }\n\n        /// <summary>\n        /// Gets the value of the binding value if present, otherwise the default value.\n        /// </summary>\n        /// <returns>The value.</returns>\n        public T? GetValueOrDefault() => HasValue ? _value : default;\n\n        /// <summary>\n        /// Gets the value of the binding value if present, otherwise a default value.\n        /// </summary>\n        /// <param name=\"defaultValue\">The default value.</param>\n        /// <returns>The value.</returns>\n        public T? GetValueOrDefault(T defaultValue) => HasValue ? _value : defaultValue;","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/BindingValue.cs#L165-L201","documentation":"Thrown as InvalidOperationException by BindingValue<T>.WithValue(T) when the current Type is BindingValueType.DoNothing. A DoNothing binding value signals the pipeline to take no action; attaching a value to it is semantically invalid. UnsetValue is allowed (it transitions to Value), but DoNothing is terminal.","triggerScenarios":"Calling bindingValue.WithValue(x) on a BindingValue whose Type == BindingValueType.DoNothing. WithValue first checks for DoNothing and throws before any other logic.","commonSituations":"Chaining WithValue on a binding value produced by a 'do nothing' sentinel without checking Type first. Composing binding results where one branch intentionally returns DoNothing and a later step tries to attach a fallback value.","solutions":["Check that Type is not DoNothing before calling WithValue; for DoNothing, propagate it unchanged.","If you need a fallback on error, construct a BindingErrorWithFallback via the factory instead of calling WithValue on a DoNothing.","Branch on Type: only call WithValue when Type is UnsetValue or Value."],"exampleFix":"// before\nvar result = donothingValue.WithValue(fallback); // throws\n// after\nvar result = donothingValue.Type == BindingValueType.DoNothing\n    ? donothingValue\n    : donothingValue.WithValue(fallback);","handlingStrategy":"validation","validationCode":"if (bindingValue.Type != BindingValueType.DoNothing)\n    bindingValue = bindingValue.WithValue(value);","typeGuard":"static bool CanAcceptValue<T>(BindingValue<T> bv) => bv.Type != BindingValueType.DoNothing;","tryCatchPattern":"try { bindingValue = bindingValue.WithValue(value); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"DoNothing\"))\n{ /* propagate the DoNothing value unchanged */ }","preventionTips":["Check Type is not DoNothing before calling WithValue.","Propagate DoNothing values unchanged through binding pipelines.","Use BindingErrorWithFallback factories for fallback semantics instead of WithValue on DoNothing."],"tags":["binding","bindingvalue","donothing","data"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}