How to access WPF Label's Target element?

Jan 6, 2011 at 11:15 AM


Can I somehow get (using White) the element defined in WPF Label's Target property (myTextBox in the sample below)?

<Label x:Name="myLabel" Target="{Binding ElementName=myTextBox}" />

Many thanks,


Jan 6, 2011 at 12:42 PM


I think you have something like this in view


Label:                   |TextBox                |




For UI Automation there is no causal context between these Items

you should get the TextBox from the parent element in view (Window, or other control)





Jan 6, 2011 at 2:00 PM

Thanks Throndorin,

My view is as you showed. The Label-text box pairs are in a Grid and the Grid is as an ItemsControl's ItemsTemplate. 
I can find a collection of myLabel and myTextBox controls using following code:


var labels = window.GetMultiple(SearchCriteria.ByAutomationId("myLabel"));
var txtBoxes = window.GetMultiple(SearchCriteria.ByAutomationId("myTextBox"));


My problem is that not each label will have a pairing text box (due to template selection).
This means that I cannot simply use the index of the 'labels' variable above to find the matching text box (labels.Count() != txtBoxes.Count()).

I would need to get a handle to the immediate parent control of myLabel control and from that try to find myTextBox. I tried following:

labels[0].AutomationElement.FindAll(TreeScope.Parent, new PropertyCondition(AutomationElement.AutomationIdProperty, "ParentGrid"));

But that gives error: Only TreeScope.Element, TreeScope.Children, and TreeScope.Descendants are valid for this call.

Any ideas how to find the TextBox that is on the same WPF Grid row as a Label that I can identify (by Label text)?






Jan 6, 2011 at 2:12 PM



are they also in the same cell?

can you show the complete structure (UIAVerify or UISpy) from parent and the needed elements? (You can also use LogStructure on parent element (maybe only the row)



Jan 6, 2011 at 3:43 PM

Ok, here is (slightly cleaned) LogStructure output of the ItemListContainer. I can find myLabel by its content ('Age' in the example) and would like to get its sibling, myTextBox.

I can find ItemListContainer using:


, but I cannot find myLabel's parent control 'LabelTxtBoxPairContainer':


returns an empty collection.



AutomationId: ItemListContainer
ControlType: ControlType.Custom



    AutomationId: LabelTxtBoxPairContainer
    ControlType: ControlType.Custom



        AutomationId: myLabel
        ControlType: ControlType.Text
        Name: Age
        Bounding rectangle: 261,275,84,26
        ClassName: Text
        IsOffScreen: False
        FrameworkId: WPF
        ProcessId: 5380

        AutomationId: myTextBox
        ControlType: ControlType.Edit
        Bounding rectangle: 461,251,250,22
        ClassName: TextBox
        IsOffScreen: False
        FrameworkId: WPF
        ProcessId: 5380





Jan 6, 2011 at 8:13 PM

I finally managed to find the sibling TextBox with with this code (labels[2] contains the label whose sibling I'm looking for):



var parent = TreeWalker.ControlViewWalker.GetParent(labels[2].AutomationElement);

var txtBox = parent.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "myTextBox"));

White.Core.UIItems.TextBox tb = new White.Core.UIItems.TextBox(txtBox, window.ActionListener);

tb.Text = "abc";





Jan 7, 2011 at 9:42 AM


the parent Class is custom I managed these things this way:


Create a CustomControl Class (see documentation)  LabelTtxtBoxPairContainer


add properties for Label and TextBox and fill the values in the constructor.

(in normal way you can use the Get operation on these custom control class)




than :


AutomationElementCollection coll =

window.FindAll( TreeScope.Descendants, new PropertyConditionAutomationElement.AutomationIdProperty, "LabelTtxtBoxPairContainer" ) );

List<LabelTxtBoxPairContainer> list  = new List<LabelTxtBoxPairContainer>();

foreach(AutomationElement element in coll)


list.Add(new LabelTxtBoxPairContainer(element, window.ActionListener)



now you can get directly the controls from the properties













Jan 7, 2011 at 4:49 PM

Thanks Throndorin,


I tried to organize the code as you showed, but now the problem is that the Container object in the new LabelTxtBoxPairContainer (CustomUIItem) is null when I try to use these custom items after creating them.

At which point 'Container' will be set by the framework or do I self have to instantiate it?



Jan 10, 2011 at 6:10 AM


the AutomationElement is null?

or the properties in the container?