 |
|
|
|
Finding a XAML Element by Name at Runtime
|
|
|
 |
|
Location: Blogs Andy's Blog |
|
| Posted by: host |
7/14/2008 4:52 PM |
In ASP.NET, it's possible to get a reference to a control by Id at runtime using the FindControl method - something like this:
Label lblCustomerName = (Label) item.FindControl("lblCustomerName");
This can be helpful in dynamic code which might need to iterate through a large collection of elements in XAML.
In Silverlight, there is no FindControl method, but we can still get a reference to an object by name using the FindName method of a container:
TextBlock lblCustomerName = cnvContainer.FindName("lblCustomerName") as TextBlock
We can also access the Children collection of a container and query using a lambda expression:
TextBlock lblCustomerName = (cnvContainer.Children.First<UIElement>(label => label.GetValue(Canvas.NameProperty).ToString() == "lblCustomerName")) as TextBlock;
In the above example, we are getting a reference to an object named lblCustomerName inside a canvas named cnvContainer. We do so by using a lambda expression on the Children property, along with the Canvas.NameProperty which gives the variable (x:Name) property of a UIElement. |
|
| Permalink |
Trackback |
Comments (1)
Add Comment
|
Re: Finding a XAML Element by Name at Runtime
|
By Anonymous on
4/17/2009 4:46 PM
|
Hi Andy - This is Tom Battista (tommybc98@yahoo.com). Are you the Andy Beaulieu I know?
|
|
|
|
 |
|
 |
|
|