dotnet/wpf · error · InvalidOperationException

SR.SetFocusFailed

Error message

SR.SetFocusFailed

What it means

DatePickerAutomationPeer.SetFocusCore throws InvalidOperationException when focus was requested on the DatePicker but neither the owner nor its internal TextBox ended up keyboard-focused. UIA's SetFocus contract requires focus to actually land, so the peer reports the failure rather than silently ignoring it.

Solutions

  1. Ensure the DatePicker is visible, enabled, and its window is active before calling SetFocus.
  2. Make sure the DatePicker's template (including its TextBox part) is loaded.
  3. Activate the containing window (Window.Activate()/SetForegroundWindow) first.
  4. Catch InvalidOperationException around SetFocus and retry after activating the window.

Example fix

// before
window.Show();
datePickerPeer.SetFocus(); // may fail if window not foreground
// after
window.Show();
window.Activate();
if (datePicker.IsVisible && datePicker.IsEnabled)
    datePicker.Focus();
Defensive patterns

Strategy: try-catch

Validate before calling

bool ready = datePicker.IsVisible && datePicker.IsEnabled && datePicker.IsLoaded && Window.GetWindow(datePicker).IsActive;

Try / catch

try { datePicker.Focus(); }
catch (InvalidOperationException) { window.Activate(); datePicker.Focus(); }

Prevention

When it happens

Trigger: Calling SetFocus (IRawElementProviderSimple) on a DatePicker whose DatePickerTextBox cannot be located (owner.TextBox is null) or which did not take keyboard focus — e.g. the control is disabled, not visible, or in a non-activated window.

Common situations: UIA tests setting focus on a DatePicker inside a collapsed/unloaded tab or disabled control; windows without activation so Win32 Focus() succeeds but IsKeyboardFocused remains false.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DatePickerAutomationPeer.cs:68

            return base.GetPattern(patternInterface);
        }

        #endregion Public Methods

        #region Protected Methods

        protected override void SetFocusCore()
        {
            DatePicker owner = OwningDatePicker;
            if (owner.Focusable)
            {
                if (!owner.Focus())
                {
                    TextBox tb = owner.TextBox;
                    //The focus should have gone to the TextBox inside DatePicker
                    if (tb == null || !tb.IsKeyboardFocused)
                    {
                        throw new InvalidOperationException(SR.SetFocusFailed);
                    }
                }
            }
            else
            {
                throw new InvalidOperationException(SR.SetFocusFailed);
            }
        }

        /// <summary>
        /// Gets the control type for the element that is associated with the UI Automation peer.
        /// </summary>
        /// <returns>The control type.</returns>
        protected override AutomationControlType GetAutomationControlTypeCore()
        {
            return AutomationControlType.Custom;
        }

View on GitHub (pinned to 81131a70a4)