Set an AutomationProperties.AutomationId in code behind
Asked Answered
A

3

5

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?

Anabolite answered 13/7, 2014 at 0:40 Comment(0)
A
13
myCheckbox.SetValue(AutomationProperties.AutomationIdProperty, "checkBoxAutoID");
Audre answered 13/7, 2014 at 4:36 Comment(0)
O
1

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 >
Ophthalmoscope answered 28/2, 2018 at 22:5 Comment(0)
W
1

Try this:

AutomationProperties.SetAutomationId(myCheckbox, "YourDesiredname");

Ofcourse you need to include the namespace.

using System.Windows.Automation;

Wende answered 28/4, 2021 at 10:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.