11

I need to automate a Winform application. How do I set the AutomationID (or AutomationName) like the the XAML in this article does?

From this stack overflow article the answer seems to be no, unless I switch the application to a WPF application (so I can use XAML to define the controls).

I have tried this naïve approach:

  AutomationElement formAutomation = AutomationElement.FromHandle(this.Handle);
  formAutomation.Current.Name = "SandboxResponseDialogName";
  formAutomation.Current.ClassName = "SandboxResponseDialogClassName";
  formAutomation.Current.AutomationId = "SandboxResponseDialogID;

But at this point in the constructor for the control, these Automation properties have getters only; no setters.

Community
  • 1
  • 1
John Washburn
  • 568
  • 1
  • 5
  • 11

1 Answers1

8

If you want to set anything in relation to UI Automation in code, you need to use this:

using System.Windows.Automation;

And in your code:

YourObjectClass element = // just get your element.
element.SetValue(AutomationProperties.AutomationIdProperty, "elementAutomationID");

You can also use AutomationProperties.NameProperty for the UIAutomation Name. AutomationProperties contains all the properties for UIAutomation elements (setter and getter) as the name suggest.

Mualig
  • 1,444
  • 1
  • 19
  • 42
  • Note that in this example `element` is not a UI Automation element (`AutomationElement`), rather a WPF control (e.g. `CheckBox`). – Ohad Schneider Dec 20 '15 at 16:27