dotnet/wpf · error · InvalidOperationException
SR.OperationCannotBePerformed
Error message
SR.OperationCannotBePerformed
What it means
WindowsScroll.SetScrollPercent first verifies the window actually has a scrollable bar via IsScrollable(hwnd); if not, it throws InvalidOperationException with SR.OperationCannotBePerformed. UIAutomation's Scroll pattern was invoked on a control that cannot be scrolled in the requested direction.
Solutions
- Check ScrollPattern.ScrollInfo / IsScrollable before calling SetScrollPercent and skip when not scrollable.
- Ensure the target control actually shows scroll bars (resize the window or populate more content) before automating.
- Catch InvalidOperationException and treat the element as non-scrollable instead of failing the automation run.
Example fix
// before
scrollPattern.SetScrollPercent(50, ScrollPattern.NoScroll);
// after
if (scrollPattern.Current.VerticallyScrollable)
scrollPattern.SetScrollPercent(ScrollPattern.NoScroll, 50); Defensive patterns
Strategy: validation
Validate before calling
var sp = (ScrollPattern)element.GetCurrentPattern(ScrollPattern.Pattern); bool ok = sp.Current.HorizontallyScrollable || sp.Current.VerticallyScrollable;
Try / catch
try { sp.SetScrollPercent(h, v); }
catch (InvalidOperationException) { /* element is not scrollable; skip */ } Prevention
- Check HorizontallyScrollable/VerticallyScrollable before SetScrollPercent
- Re-read scrollability after layout or content changes
- Handle auto-hidden scroll bars by forcing content overflow in test fixtures
When it happens
Trigger: Calling the Scroll pattern's SetScrollPercent (ScrollPattern.SetScrollPercent) on a window whose scroll bars are disabled or non-scrollable (IsScrollable returns false), detected at WindowsScroll.cs:31.
Common situations: Automating a list/view whose content currently fits without scrolling; scroll bars hidden because 'auto-hide scroll bars' is on; targeting a child control that has no scroll style bits (WS_HSCROLL/WS_VSCROLL).
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
- SR.OperationCannotBePerformed
- SR.ScrollBarOutOfRange
- throw new ArgumentOutOfRangeException(IsHorizontal(_hwnd) ?…
- throw new…
- Animation_Invalid_DefaultValue
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/9bd272a9368d0f8b.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsScroll.cs:31
namespace MS.Internal.AutomationProxies
{
// Static class used to support the Scroll pattern for controls that have scroll bars.
internal static class WindowScroll
{
#region Internal Methods
// ------------------------------------------------------
//
// Internal Methods
//
// ------------------------------------------------------
// Request to scroll Horizontally and vertically by the specified amount
internal static void SetScrollPercent (IntPtr hwnd, double horizontalPercent, double verticalPercent, bool forceResults)
{
if (!IsScrollable(hwnd))
{
throw new InvalidOperationException(SR.OperationCannotBePerformed);
}
bool resultsNoCheck;
bool isHorizontal = SetScrollPercent (hwnd, horizontalPercent, NativeMethods.SB_HORZ, out resultsNoCheck);
// This is needed for Controls that do not return the proper return codes to WinAPI Calls
if (!isHorizontal && (forceResults && resultsNoCheck))
{
isHorizontal = true;
}
bool isVertical = SetScrollPercent (hwnd, verticalPercent, NativeMethods.SB_VERT, out resultsNoCheck);
// This is needed for Controls that do not return the proper return codes to WinAPI Calls
if (!isVertical && (forceResults && resultsNoCheck))
{
isVertical = true;
}View on GitHub (pinned to 81131a70a4)