{"record":{"id":"f5a93913a9ac2258","repo":"dotnet/wpf","slug":"sr-optimalparagraphmustwrap","errorCode":null,"errorMessage":"SR.OptimalParagraphMustWrap","messagePattern":"SR\\.OptimalParagraphMustWrap","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/TextFormatterImp.cs","lineNumber":376,"sourceCode":"            FormatSettings settings = PrepareFormatSettings(\n                textSource,\n                firstCharIndex,\n                paragraphWidth,\n                paragraphProperties,\n                previousLineBreak,\n                textRunCache,\n                true,   // optimalBreak\n                false,  // !isSingleLineFormatting\n                _textFormattingMode\n                );\n\n            //\n            // Optimal paragraph formatting session specific check\n            //\n            if (!settings.Pap.Wrap && settings.Pap.OptimalBreak)\n            {\n                // Optimal paragraph must wrap.\n                throw new ArgumentException(SR.OptimalParagraphMustWrap);\n            }\n\n            // create paragraph content cache object\n            return new TextParagraphCache(\n                settings,\n                firstCharIndex,\n                RealToIdeal(paragraphWidth)\n                );\n        }\n\n\n\n        /// <summary>\n        /// Validate all the relevant text formatting initial settings and package them\n        /// </summary>\n        private FormatSettings PrepareFormatSettings(\n            TextSource                  textSource,\n            int                         firstCharIndex,","sourceCodeStart":358,"sourceCodeEnd":394,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/TextFormatterImp.cs#L358-L394","documentation":"TextFormatterImp.CreateParagraphCache throws ArgumentException(SR.OptimalParagraphMustWrap) when a TextFormattingSettings/ParagraphProperties specifies OptimalBreak=true but Wrap=false. Optimal paragraph formatting (line breaking algorithm) inherently depends on wrapping to compute optimal break points, so the combination is rejected.","triggerScenarios":"Setting TextParagraphProperties.OptimalBreak to true while TextWrapping is set to NoWrap (TextWrapping.NoWrap on a TextBlock or ParagraphProperties), then creating an optimal-formatting paragraph cache.","commonSituations":"Enabling optimal paragraph algorithm (for justified CJK/Korean text) on a TextBlock with TextWrapping.NoWrap; copy-pasting style settings that disable wrap; dynamic styling toggling wrap off while OptimalBreak stays on.","solutions":["Set TextWrapping to Wrap (or WrapWithOverflow) whenever OptimalBreak is true.","Set OptimalBreak = false if the text is intentionally non-wrapping.","Add a guard in property-change handlers so OptimalBreak and Wrap are kept consistent."],"exampleFix":"// before\nvar paraProps = new TextParagraphProperties {\n    TextWrapping = TextWrapping.NoWrap,\n    OptimalBreak = true // throws\n};\n// after\nvar paraProps = new TextParagraphProperties {\n    TextWrapping = TextWrapping.Wrap,\n    OptimalBreak = true\n};","handlingStrategy":"validation","validationCode":"if (paraProps.OptimalBreak && paraProps.TextWrapping == TextWrapping.NoWrap)\n    paraProps.TextWrapping = TextWrapping.Wrap; // or set OptimalBreak = false","typeGuard":"bool OptimalBreakIsValid(bool optimalBreak, TextWrapping wrap) => !optimalBreak || wrap != TextWrapping.NoWrap;","tryCatchPattern":"try { formatter.FormatLine(source, start, width, paraProps, null); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Optimal\")) { paraProps.TextWrapping = TextWrapping.Wrap; /* retry */ }","preventionTips":["Treat OptimalBreak and TextWrapping as a coupled setting; change them together.","Add style triggers that disable OptimalBreak when wrapping is off.","Validate paragraph properties in a helper factory instead of inline construction."],"tags":["wpf","textformatting","textblock","conflicting-options"],"backgroundTag":"conflicting-config-options","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"}