Quak
Quak

Reputation: 7453

Searching controls by name in Windows Phone c# application

Is it possible to search controls by name in Windows Phone application? I know it's possible in normal desktop application written in c#:

string s = "label2";
            Control[] controls = this.Controls.Find(s, false);
            if (controls.Length > 0)
            {
                Label found = controls[0] as Label;
                if (found != null) found.Text = "new label2 text";
            }

In Windows Phone application 'this.Controls' is unrecognized...

Upvotes: 0

Views: 763

Answers (2)

ColinE
ColinE

Reputation: 70142

Yes, it is possible - but with Windows Phone your controls are located within a tree-like structure called the visual tree. In order to find a control, you have to navigate this tree. I wrote a utility class a while bcd called Linq-to-VisualTree that simplifies this process.

Upvotes: 1

Kevin Gosse
Kevin Gosse

Reputation: 39007

Use FindName:

var control = this.FindName(s);

if (control != null)
{
    // Whatever
}

Upvotes: 3

Related Questions