{"record":{"id":"17120dd7e86ea39e","repo":"dotnet/wpf","slug":"sr-documentviewerargumentmustbepercentage","errorCode":null,"errorMessage":"SR.DocumentViewerArgumentMustBePercentage","messagePattern":"SR\\.DocumentViewerArgumentMustBePercentage","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DocumentViewer.cs","lineNumber":1960,"sourceCode":"            // Check that args is valid\n            ArgumentNullException.ThrowIfNull(data);\n\n            // If a ZoomConverter doesn't exist, create one.\n            if (dv._zoomPercentageConverter == null)\n            {\n                dv._zoomPercentageConverter = new ZoomPercentageConverter();\n            }\n\n            // Use ZoomConverter to convert argument to zoom value.\n            // We use InvariantCulture because the Command arguments are typically\n            // defined in XAML or code, which is culture invariant.\n            object zoomValue = dv._zoomPercentageConverter.ConvertBack(data, typeof(double),\n                null, CultureInfo.InvariantCulture);\n\n            // Argument wasn't a valid percent, throw an exception.\n            if (zoomValue == DependencyProperty.UnsetValue)\n            {\n                throw new ArgumentException(SR.DocumentViewerArgumentMustBePercentage, nameof(data));\n            }\n            dv.Zoom = (double)zoomValue;\n        }\n\n        #endregion Commands\n\n        /// <summary>\n        /// Register our properties' metadata so that our DependencyProperties function.\n        /// </summary>\n        private static void RegisterMetadata()\n        {\n            DefaultStyleKeyProperty.OverrideMetadata(typeof(DocumentViewer), new FrameworkPropertyMetadata(typeof(DocumentViewer)));\n            _dType = DependencyObjectType.FromSystemTypeInternal(typeof(DocumentViewer));\n        }\n\n        /// <summary>\n        /// Initializes our DocumentScrollInfo.\n        /// </summary>","sourceCodeStart":1942,"sourceCodeEnd":1978,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DocumentViewer.cs#L1942-L1978","documentation":"The FitToWidth (zoom percentage) command handler converts its parameter back to a double using the viewer's ZoomPercentageConverter; if the converter returns DependencyProperty.UnsetValue the argument is not a valid percentage and ArgumentException (SR.DocumentViewerArgumentMustBePercentage) is thrown naming data.","triggerScenarios":"Executing FitToWidthCommand with a parameter the ZoomPercentageConverter cannot parse (e.g. \"abc\", \"150%%\", unsupported type); replacing the default ZoomPercentageConverter with one that rejects the input.","commonSituations":"Toolbar zoom combo boxes emitting formatted strings like \"75 %\" that the converter does not accept; custom converters returning UnsetValue for localized input.","solutions":["Pass a valid percentage string (e.g. \"75\") or double as the command parameter","Verify ZoomPercentageConverter (custom ones included) accepts the formats your UI produces","Parse/normalize the zoom input in the view model before invoking the command"],"exampleFix":"// before\nzoomButton.CommandParameter = string.Format(\"{0} %\", zoom);\n// after\nzoomButton.CommandParameter = zoom.ToString(CultureInfo.InvariantCulture);","handlingStrategy":"validation","validationCode":"var conv = new ZoomPercentageConverter();\nbool ok = conv.ConvertBack(data, typeof(double), null, CultureInfo.InvariantCulture) != DependencyProperty.UnsetValue;","typeGuard":null,"tryCatchPattern":"try { viewer.FitToWidthCommand.Execute(data); }\ncatch (ArgumentException) { /* data is not a valid percentage */ }","preventionTips":["Use the same converter for UI input as the command does","Normalize localized percentage strings before invoking","Validate zoom inputs at the view-model layer"],"tags":["wpf","documentviewer","command","argument-exception"],"backgroundTag":"invalid-argument-format","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"}