Solved

How can I find a UIElement on a WPF application

Posted on 2010-08-26
4
783 Views
Last Modified: 2013-11-12
Hi,

I have a number of Ellipses on the main interface to a WPF application.

I would like to change the properties of these programmatically, but I want to reference them by name (otherwise the code will be huge!!)

I would like something like this:

   
Ellipse mine = (Ellipse) UIElements.Find["ellipse12"];

Open in new window


I know this is not the correct syntax, but is this approach possible???

Any help would be appreciated.

Best regards,

James
0
Comment
Question by:jatkin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:Silverlay
ID: 33531262
You can archive that by making your own wrap over VisualTreeHelper class in Silverlight.

Helpfull links on that class that have some use ready links:
http://blogs.msdn.com/b/kmahone/archive/2009/03/29/visualtreehelper.aspx
http://amyokabir.wordpress.com/2010/03/01/my-visualtreehelper-search-the-visual-tree/
0
 
LVL 4

Author Comment

by:jatkin
ID: 33531610
Hi,

Thanks for the info..
I have found that the following does this directly...

    Int32 xElipseID = 3;
    Ellipse myEllipse = (Ellipse) FindName("ellipse" + xElipseID.ToString());

Is this *correct* WPF coding ?

James
0
 
LVL 5

Accepted Solution

by:
Silverlay earned 500 total points
ID: 33541021
It's all correct till it's works :P

But in case of your code you can easily get null, especialy with dynamicly created objects. VisualTreeHelper handles most common errors and FindName is failsafe and requires sainty checks.
0
 
LVL 4

Author Closing Comment

by:jatkin
ID: 33541121
Hee hee - point taken :-) Thanks
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Explain Unit of Work pattern 2 76
WPF issue with Trigger 2 130
Is IHttpActionResult a promise pattern? 2 74
Need a complete list of ASP.NET page load events 3 64
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
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…

733 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