Solved

How can I find a UIElement on a WPF application

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual Studio Exe Issue, 12 25
Table Size and Indexes in SQL Server 6 20
TFS Branching 4 63
dialer.exe 4 52
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now