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(myRectangle);

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?  
VoodooFrogAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mikebirtCommented:
Hi,

when you put stuff into Xaml a g.cs file is created behind the scenes (like the designer.cs in win forms) which has the instances of your controls. These instances are what you're using in your code. The way you've created your rectangle instance is using 'myRectangle' as it's instance name. 'rec1' is not the instance name of the object. you need to refer to the rectangle as myRectangle, just like any other object and store it within the class scope. If you need some named way of accessing them i think you'll need to create a Dictionary<string,Rectangle> and reference them from there.

HTH

Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EDDYKTCommented:
have you tried

Rectangle t = (Rectangle)this.FindName("rec1");
0
mikebirtCommented:
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
            string rectangleName = "rec";
            Rectangle r = new Rectangle();
            r.Name = rectangleName;

            myCanvas.Children.Add(r);

            this.RegisterName(rectangleName, r);

            Rectangle rAgain = (Rectangle)FindName(rectangleName);

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

VoodooFrogAuthor Commented:
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.  

0
mikebirtCommented:
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
0
VoodooFrogAuthor Commented:
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.  
0
VoodooFrogAuthor Commented:
the dictionary seems to be the simplest way reall -- registerName doesn't seem to work for some reason.  
0
EDDYKTCommented:
don't think you need to register name,

just do

Rectangle t = (Rectangle)this.FindName("rec1");
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.