VoodooFrog
asked on
Refer to Silverlight Control by name or tag in C#
I have a couple of controls that I created in c# -- they are not declared in XAML. I am looking for a way to refer to these controls programatically.
for instance, I create a rectangle and assign a few properties including the name and tag attributes:
Rectangle myRectangle = new Rectangle();
myRectangle.Name = "rec1";
myRectangle.Tag = "tagRec1";
myCanvas.Children.Add(myRe ctangle);
I create several rectangles and want to be able to refer to them later on in code. However, I get an error trying to refer to them as I would an object declared in XAML.
rec1.Opacity for example.
The error is 'the name 'rec1' does not exist in the current context'
How do I refer to these objects once they are created?
for instance, I create a rectangle and assign a few properties including the name and tag attributes:
Rectangle myRectangle = new Rectangle();
myRectangle.Name = "rec1";
myRectangle.Tag = "tagRec1";
myCanvas.Children.Add(myRe
I create several rectangles and want to be able to refer to them later on in code. However, I get an error trying to refer to them as I would an object declared in XAML.
rec1.Opacity for example.
The error is 'the name 'rec1' does not exist in the current context'
How do I refer to these objects once they are created?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
Good idea EDDYKT! that got me thinking... you can't use the FindName still, you have to call RegisterName first. Then you can use FindName
HTH
Mike
Good idea EDDYKT! that got me thinking... you can't use the FindName still, you have to call RegisterName first. Then you can use FindName
HTH
Mike
string rectangleName = "rec";
Rectangle r = new Rectangle();
r.Name = rectangleName;
myCanvas.Children.Add(r);
this.RegisterName(rectangleName, r);
Rectangle rAgain = (Rectangle)FindName(rectangleName);
ASKER
thank you -- this is a home project, so I will try these things tonight after work. It looks like that is the solution that I am looking for though. -- registering the name looks like it will do that.
Hi,
If you register the name you can use EDDYKT's idea of FindName, but you still can't use the 'this' instance to reference it directly. In my previous code example, this.rec wouldn't work, you must use FindName("rec").
HTH
Mike
If you register the name you can use EDDYKT's idea of FindName, but you still can't use the 'this' instance to reference it directly. In my previous code example, this.rec wouldn't work, you must use FindName("rec").
HTH
Mike
ASKER
I am getting a failure on the registering the rectangle name. Says that the 'SLapp1 does not contain a definition for 'RegisterName'
In looking up the reference to RegisterName is says that the Namespace is System.Windows which is already incuded in the project.
In looking up the reference to RegisterName is says that the Namespace is System.Windows which is already incuded in the project.
ASKER
the dictionary seems to be the simplest way reall -- registerName doesn't seem to work for some reason.
don't think you need to register name,
just do
Rectangle t = (Rectangle)this.FindName(" rec1");
just do
Rectangle t = (Rectangle)this.FindName("
Rectangle t = (Rectangle)this.FindName("