Solved

How can I find a UIElement on a WPF application

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

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

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 …
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

707 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