doobop
doobop

Reputation: 4520

Set an AutomationProperties.AutomationId in code behind

I have a WPF application that has a few legacy panels that were written mostly with code behind. I need to set the AutomationProperties.AutomationId for the controls on the panel. For example, this Checkbox

CheckBox myCheckbox = new CheckBox();

How can I set AutomationProperties.AutomationId?

Upvotes: 5

Views: 5757

Answers (3)

Ashfaq Ahmed
Ashfaq Ahmed

Reputation: 11

Try this:

AutomationProperties.SetAutomationId(myCheckbox, "YourDesiredname");

Ofcourse you need to include the namespace.

using System.Windows.Automation;

Upvotes: 1

ChadSixt
ChadSixt

Reputation: 56

You have to add the Name property "myCheckbox" to the xaml checkbox definition to use the following LOC:

myCheckbox.SetValue(AutomationProperties.AutomationIdProperty, "checkBoxAutoID");

You can alternatively define in xaml:

<CheckBox x:Name="myCheckbox" 
... AutomationProperties.AutomationId="mycheckBoxAutoID" ...
</CheckBox >

Upvotes: 1

d.moncada
d.moncada

Reputation: 17402

myCheckbox.SetValue(AutomationProperties.AutomationIdProperty, "checkBoxAutoID");

Upvotes: 13

Related Questions