Solved

How can I find a UIElement on a WPF application

Posted on 2010-08-26
4
782 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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 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