Page view counter

Did You Know That... there are numerous ways to find an object inside a Silverlight control

Is very common to need to access a Silverlight element programmatically. There are numerous ways to do so. Assuming you have named your element as shown here
<Rectangle Name="myRect"
           Width="100"
           Height="44"
           Canvas.Left="0"
           Canvas.Top="10"
           StrokeThickness="2"
           Stroke="Black" />

One very simple way to access this rectangle from the handleLoad function is by using getItem() and indexing into the children collection of the root element

this.rect = rootElement.children.getItem(0);
this.rect.strokeThickness="5";

An alternative and more general approach is to use findName(),  which you can also call on any element, including the root element

this.rect2 = rootElement.FindName("myRect");
this.rect2.Fill = "Red";

Note this quote from the documentation: the object you are searching for does not have to be a direct descendant of the object that invokes the findName method.

In addition, you can call FindName() on the plugin's content sub-object

var rect3 = plugIn.content.FindName("myRect");
rect3["Canvas.Left"] = 300;

Published Thursday, January 03, 2008 10:12 AM by jesseliberty

Comments

No Comments