?
Solved

Refer to Silverlight Control by name or tag in C#

Posted on 2010-04-08
8
Medium Priority
?
400 Views
Last Modified: 2013-11-26
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?  
0
Comment
Question by:VoodooFrog
  • 3
  • 3
  • 2
8 Comments
 
LVL 5

Accepted Solution

by:
mikebirt earned 2000 total points
ID: 30189970
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 30201284
have you tried

Rectangle t = (Rectangle)this.FindName("rec1");
0
 
LVL 5

Expert Comment

by:mikebirt
ID: 30202859
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:VoodooFrog
ID: 30203943
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
 
LVL 5

Expert Comment

by:mikebirt
ID: 30204133
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
 

Author Comment

by:VoodooFrog
ID: 30256100
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
 

Author Comment

by:VoodooFrog
ID: 30267986
the dictionary seems to be the simplest way reall -- registerName doesn't seem to work for some reason.  
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 30409874
don't think you need to register name,

just do

Rectangle t = (Rectangle)this.FindName("rec1");
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question