{"record":{"id":"49f1f134535fdaad","repo":"AvaloniaUI/Avalonia","slug":"two-way-bindings-are-not-supported-with-a-string-f","errorCode":null,"errorMessage":"Two way bindings are not supported with a string format","messagePattern":"Two way bindings are not supported with a string format","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/Converters/StringFormatValueConverter.cs","lineNumber":49,"sourceCode":"        /// </summary>\n        public string Format { get; }\n\n        /// <inheritdoc/>\n        public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)\n        {\n            value = Inner?.Convert(value, targetType, parameter, culture) ?? value;\n            var format = Format!;\n            if (!format.Contains('{'))\n            {\n                format = $\"{{0:{format}}}\";\n            }\n            return string.Format(culture, format, value);\n        }\n\n        /// <inheritdoc/>\n        public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)\n        {\n            throw new NotSupportedException(\"Two way bindings are not supported with a string format\");\n        }\n    }\n}\n","sourceCodeStart":31,"sourceCodeEnd":53,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/Converters/StringFormatValueConverter.cs#L31-L53","documentation":"StringFormatValueConverter applies a display format (string.Format) to a value flowing source-to-target. It is inherently one-directional: there is no reliable way to reverse-parse a formatted string back into the original source value, so ConvertBack unconditionally throws NotSupportedException. The library throws it to fail fast rather than silently produce wrong data on the source side.","triggerScenarios":"A binding with BindingMode.TwoWay or BindingMode.OneWayToSource whose Binding/StringFormatValueConverter also has a non-null StringFormat. The mode default for some targets (e.g. TextBox.Text) is TwoWay, so simply adding a StringFormat to such a binding triggers it even when Mode is left unset.","commonSituations":"Adding StringFormat='C' or StringFormat='N2' to a TwoWay TextBox.Text binding to pretty-print currency/numbers; enabling StringFormat on a TwoWay Slider.Value or ComboBox.SelectedItem binding; upgrading a OneWay binding to TwoWay without removing the format.","solutions":["Set the binding Mode to OneWay (or OneTime) so ConvertBack is never invoked.","Remove StringFormat and instead expose a pre-formatted string property on the viewmodel, or format in the target's own display logic.","Keep TwoWay but drop the StringFormat and apply formatting through a custom IValueConverter that implements a real ConvertBack parse step."],"exampleFix":"// before\n<TextBox Text=\"{Binding Amount, StringFormat=C, Mode=TwoWay}\" />\n// after - OneWay display cannot edit; for editable, drop the format\n<TextBox Text=\"{Binding Amount, Mode=TwoWay}\" />\n<TextBlock Text=\"{Binding Amount, StringFormat=C}\" />","handlingStrategy":"validation","validationCode":"// Validate before applying: StringFormat is only valid on one-way display bindings.\nBinding ValidateStringFormat(Binding b)\n{\n    if (!string.IsNullOrEmpty(b.StringFormat) &&\n        (b.Mode == BindingMode.TwoWay || b.Mode == BindingMode.OneWayToSource))\n    {\n        throw new InvalidOperationException(\n            \"StringFormat cannot be used with TwoWay/OneWayToSource bindings.\");\n    }\n    // Mode.Default may resolve to TwoWay for some targets (e.g. TextBox.Text);\n    // force OneWay when a format is present unless you are sure.\n    if (!string.IsNullOrEmpty(b.StringFormat) && b.Mode == BindingMode.Default)\n        b.Mode = BindingMode.OneWay;\n    return b;\n}","typeGuard":null,"tryCatchPattern":"// ConvertBack is on the hot path; prefer the validation above. If you wrap converters:\ntry { var back = converter.ConvertBack(v, t, p, c); }\ncatch (NotSupportedException) { /* StringFormat/one-way converter: ignore write-back */ }","preventionTips":["Treat StringFormat as display-only: pair it with OneWay/OneTime bindings.","Remember TextBox.Text, Selector.SelectedItem, and similar default to TwoWay.","Format on a separate display TextBlock instead of the editable control."],"tags":["data-binding","string-format","converter","two-way"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}