dotnet/wpf · error · InvalidOperationException

throw new…

Error message

throw new InvalidOperationException(SR.OperationCannotBePerformed);

What it means

After passing the enabled check, SetValue parses the string with DateTime.Parse (current culture) and calls the private SetValue(DateTime). If that native operation fails (returns false), the proxy throws InvalidOperationException with SR.OperationCannotBePerformed, indicating the control refused the value.

Solutions

  1. Format the value using CultureInfo.CurrentCulture (e.g. DateTime.Now.ToString("d", CultureInfo.CurrentCulture)) so DateTime.Parse succeeds.
  2. Ensure the date is within the control's MinDate/MaxDate range.
  3. Use UIA SendKeys on the control instead of ValuePattern if the native SetValue path keeps failing.

Example fix

// before
valuePattern.SetValue("2024-01-02");
// after
string s = new DateTime(2024, 1, 2).ToString("d", CultureInfo.CurrentCulture);
valuePattern.SetValue(s);
Defensive patterns

Strategy: validation

Validate before calling

DateTime dt;
if (!DateTime.TryParse(text, CultureInfo.CurrentCulture, DateTimeStyles.None, out dt)) throw new FormatException("Value must be a culture-valid date: " + text);
if (dt < picker.MinDate || dt > picker.MaxDate) throw new ArgumentOutOfRangeException(nameof(text));

Try / catch

try { valuePattern.SetValue(text); } catch (InvalidOperationException) { /* correct format/range and retry */ }

Prevention

When it happens

Trigger: Calling ValuePattern.SetValue with a string that fails DateTime.Parse under the current culture (wrong format, e.g. "2024-01-02" in a MM/dd/yyyy culture), or a date the DateTimePicker natively rejects (out of MinDate/MaxDate range).

Common situations: Culture mismatch between test machine and app (locale-dependent date strings), setting dates outside the control's configured range, passing time-only or empty strings.

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/1a4bb0805f25f295. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/WindowsDateTimePicker.cs:221

            return null;
        }

        #endregion

        #region Value Pattern

        // Sets the text in the edit part of the Combo
        void IValueProvider.SetValue (string val)
        {
            // Make sure that the control is enabled
            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
            {
                throw new ElementNotEnabledException();
            }

            if (!SetValue(DateTime.Parse(val, CultureInfo.CurrentCulture)))
            {
                throw new InvalidOperationException(SR.OperationCannotBePerformed);
            }
        }

        // Request to set the value that this UI element is representing as a string
        string IValueProvider.Value
        {
            get
            {
                int cLen = Misc.ProxySendMessageInt(_hwnd, NativeMethods.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
                if (cLen > 0)
                {
                    cLen++;
                    StringBuilder sb = new StringBuilder (cLen);

                    Misc.ProxySendMessage(_hwnd, NativeMethods.WM_GETTEXT, new IntPtr(cLen), sb);

                    return sb.ToString ();
                }

View on GitHub (pinned to 81131a70a4)