dotnet/wpf · error · InvalidOperationException

SR.UIA_OperationCannotBePerformed

Error message

SR.UIA_OperationCannotBePerformed

What it means

ScrollViewerAutomationPeer.Scroll throws InvalidOperationException(SR.UIA_OperationCannotBePerformed) when a scroll amount is requested on an axis that is not scrollable. If horizontal scrolling is requested while HorizontallyScrollable is false, or vertical while VerticallyScrollable is false, the operation cannot be performed.

Solutions

  1. Check the Scroll pattern's HorizontallyScrollable/VerticallyScrollable properties before calling Scroll
  2. Pass ScrollAmount.NoAmount for the non-scrollable axis
  3. Ensure content is large enough / ScrollViewer allows scrolling on that axis
  4. Catch InvalidOperationException and treat as no-op

Example fix

// before
scrollPattern.Scroll(ScrollAmount.SmallIncrement, ScrollAmount.NoAmount);
// after
if (scrollPattern HorizontallyScrollable)
    scrollPattern.Scroll(ScrollAmount.SmallIncrement, ScrollAmount.NoAmount);
Defensive patterns

Strategy: validation

Validate before calling

var sp = (IScrollProvider)peer.GetPattern(PatternInterface.Scroll);
bool ok = (h == ScrollAmount.NoAmount || sp.HorizontallyScrollable)
       && (v == ScrollAmount.NoAmount || sp.VerticallyScrollable);

Type guard

static bool CanScrollAxis(IScrollProvider sp, ScrollAmount h, ScrollAmount v) =>
    (h == ScrollAmount.NoAmount || sp.HorizontallyScrollable) &&
    (v == ScrollAmount.NoAmount || sp.VerticallyScrollable);

Try / catch

try { sp.Scroll(h, v); }
catch (InvalidOperationException) { /* axis not scrollable */ }

Prevention

When it happens

Trigger: Calling Scroll with a non-NoAmount horizontal amount when ScrollViewer.HorizontalScrollBarVisibility/extent makes it non-scrollable (ScrollUnit-based HorizontallyScrollable property false), or same for vertical.

Common situations: Requesting horizontal scroll on a viewer whose content fits horizontally (scrollbar hidden); scripts assuming both axes are scrollable.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ScrollViewerAutomationPeer.cs:85

        /// <summary>
        /// Request to scroll horizontally and vertically by the specified amount.
        /// The ability to call this method and simultaneously scroll horizontally
        /// and vertically provides simple panning support.
        /// </summary>
        /// <see cref="IScrollProvider.Scroll"/>
        void IScrollProvider.Scroll(ScrollAmount horizontalAmount, ScrollAmount verticalAmount)
        {
            if(!IsEnabled())
                throw new ElementNotEnabledException();

            bool scrollHorizontally = (horizontalAmount != ScrollAmount.NoAmount);
            bool scrollVertically = (verticalAmount != ScrollAmount.NoAmount);

            ScrollViewer owner = (ScrollViewer)Owner;

            if (scrollHorizontally && !HorizontallyScrollable || scrollVertically && !VerticallyScrollable)
            {
                throw new InvalidOperationException(SR.UIA_OperationCannotBePerformed);
            }

            switch (horizontalAmount)
            {
                case ScrollAmount.LargeDecrement:
                    owner.PageLeft();
                    break;
                case ScrollAmount.SmallDecrement:
                    owner.LineLeft();
                    break;
                case ScrollAmount.SmallIncrement:
                    owner.LineRight();
                    break;
                case ScrollAmount.LargeIncrement:
                    owner.PageRight();
                    break;
                case ScrollAmount.NoAmount:
                    break;

View on GitHub (pinned to 81131a70a4)