dotnet/wpf · error · ArgumentException

SR.DocumentViewerArgumentMustBeInteger

Error message

SR.DocumentViewerArgumentMustBeInteger

What it means

The static command handler for FitToMaxPagesAcross parses its command parameter (data) as an integer; when parsing fails it throws ArgumentException (SR.DocumentViewerArgumentMustBeInteger) naming the data parameter. This guards command routing where the parameter comes from arbitrary XAML/tool input.

Solutions

  1. Ensure the CommandParameter is an int (or a string parseable via int.TryParse)
  2. Bind CommandParameter to an integer property in the view model
  3. Handle conversion in a value converter before the command executes

Example fix

// before
button.CommandParameter = "three";
// after
button.CommandParameter = 3;
Defensive patterns

Strategy: validation

Validate before calling

if (int.TryParse(param as string, out int n)) { /* safe to pass to FitToMaxPagesAcrossCommand */ }

Type guard

bool IsIntParam(object p) => p is int || (p is string s && int.TryParse(s, out _));

Try / catch

try { viewer.FitToMaxPagesAcrossCommand.Execute(param); }
catch (ArgumentException) { /* parameter was not an integer */ }

Prevention

When it happens

Trigger: Invoking DocumentViewer.FitToMaxPagesAcrossCommand with a non-integer parameter (e.g. string "abc", double, or null that fails parse); XAML Button CommandParameter bound to non-int data.

Common situations: CommandParameter bound to a text field; toolbar buttons passing percentages or formatted strings to the pages-across command.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/65928d0b21eb7643. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DocumentViewer.cs:1927

                // Catch only the expected parse exceptions
                catch (ArgumentNullException)
                {
                    isValidArg = false;
                }
                catch (FormatException)
                {
                    isValidArg = false;
                }
                catch (OverflowException)
                {
                    isValidArg = false;
                }
            }

            // Argument wasn't a valid int, throw an exception.
            if (!isValidArg)
            {
                throw new ArgumentException(SR.DocumentViewerArgumentMustBeInteger, nameof(data));
            }

            dv.OnFitToMaxPagesAcrossCommand(columnValue);
        }

        /// <summary>
        /// Helper for the Zoom Command, called from ExecutedRoutedEventHandler.
        /// Verifies that the data passed into ExecutedRoutedEventHandler is valid for a zoom factor
        /// and sets the Zoom property appropriately.
        /// </summary>
        /// <param name="dv">The DocumentViewer that recieved the command</param>
        /// <param name="data">the data associated with this command</param>
        private static void DoZoom(DocumentViewer dv, object data)
        {
            // Check that args is valid
            ArgumentNullException.ThrowIfNull(data);

            // If a ZoomConverter doesn't exist, create one.

View on GitHub (pinned to 81131a70a4)