dotnet/wpf · error · ArgumentException
SR.DocumentViewerArgumentMustBePercentage
Error message
SR.DocumentViewerArgumentMustBePercentage
What it means
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.
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
Example fix
// before
zoomButton.CommandParameter = string.Format("{0} %", zoom);
// after
zoomButton.CommandParameter = zoom.ToString(CultureInfo.InvariantCulture); Defensive patterns
Strategy: validation
Validate before calling
var conv = new ZoomPercentageConverter(); bool ok = conv.ConvertBack(data, typeof(double), null, CultureInfo.InvariantCulture) != DependencyProperty.UnsetValue;
Try / catch
try { viewer.FitToWidthCommand.Execute(data); }
catch (ArgumentException) { /* data is not a valid percentage */ } Prevention
- 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
When it happens
Trigger: 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.
Common situations: Toolbar zoom combo boxes emitting formatted strings like "75 %" that the converter does not accept; custom converters returning UnsetValue for localized input.
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
- SR.DocumentViewerArgumentMustBeInteger
- ArgumentOutOfRangeException (timeout was Duration.Automatic)
- Cannot have empty collection of DocumentPageView objects.
- Collection_BadRank
- Collection_CopyTo_ArrayCannotBeMultidimensional
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/17120dd7e86ea39e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DocumentViewer.cs:1960
// Check that args is valid
ArgumentNullException.ThrowIfNull(data);
// If a ZoomConverter doesn't exist, create one.
if (dv._zoomPercentageConverter == null)
{
dv._zoomPercentageConverter = new ZoomPercentageConverter();
}
// Use ZoomConverter to convert argument to zoom value.
// We use InvariantCulture because the Command arguments are typically
// defined in XAML or code, which is culture invariant.
object zoomValue = dv._zoomPercentageConverter.ConvertBack(data, typeof(double),
null, CultureInfo.InvariantCulture);
// Argument wasn't a valid percent, throw an exception.
if (zoomValue == DependencyProperty.UnsetValue)
{
throw new ArgumentException(SR.DocumentViewerArgumentMustBePercentage, nameof(data));
}
dv.Zoom = (double)zoomValue;
}
#endregion Commands
/// <summary>
/// Register our properties' metadata so that our DependencyProperties function.
/// </summary>
private static void RegisterMetadata()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DocumentViewer), new FrameworkPropertyMetadata(typeof(DocumentViewer)));
_dType = DependencyObjectType.FromSystemTypeInternal(typeof(DocumentViewer));
}
/// <summary>
/// Initializes our DocumentScrollInfo.
/// </summary>View on GitHub (pinned to 81131a70a4)