awesomeguy
awesomeguy

Reputation: 290

Can I refer to a WPF element by a string?

I want to be able to refer to a WPF element in C# via the text in a string. Something like this:

SelectElementFromString("TestButton").Opacity = 1;

Can I do this?

Upvotes: 3

Views: 3126

Answers (4)

Mohamed Abo AL Kear
Mohamed Abo AL Kear

Reputation: 27

   string indx ="textbox1";
var txt= layout.FindName(indx);
             TextBox sometxt= txt as TextBox;
             sometxt.Opacity = 1;

this could help

Upvotes: 0

Ed Swangren
Ed Swangren

Reputation: 124632

Do you really need to? Somehow I doubt it. How about:

XAML:


<SomeType Name="_SomeControl" .../>

Code:


_SomeControl.SomeMethod();

Using strings in that manner is almost always a bad idea and serves only to make your code fragile (I say almost because there are some valid reasons, they are just not common). Also, why does your method return a string?

EDIT: Based on your comment here I would suggest that you simply use a collection to iterate through your controls:

I would use that, but it is a pain to go [blah0.Property = true; blah 1.Property = true...] Is there some solution for this that I am missing

Almost every beginner programmer wants to do this (including myself some number of years ago). Create a bunch of controls named something like label0, label1, label2, etc. and then access them in a loop like this:

for( int i = 0; i < numControls; ++i )
{
    Control c = GetControl( "label" + i );
}

This is bad for various reasons and (luckily) completely unnecessary. Load all of your controls into a List<T> and iterate through them as needed.

private List<Whatever> _controls = new List<Whatever>();
private void InitControlList()
{
    _controls.Add( someControl );
    _controls.Add( someOtherControl );
    // and so on.  Now just iterate through the list 
    // when you need to update or access the controls.
}

As noted by H.B., my answer helps the OP, but may not help future people searching this forum who actually need a direct answer. So, here is a repost of Jon's answer (thanks, would throw you some more rep if I could =D)

You can search the child controls of a FrameworkElement with FindName. To set the name for a control, use the Name or x:Name property from XAML.

Note: If you use x:Name, the control will also be available as a field on the codebehind for your FrameworkElement, and you will be able to access it directly.

Upvotes: 5

Jon
Jon

Reputation: 437336

You can search the child controls of a FrameworkElement with FindName. To set the name for a control, use the Name or x:Name property from XAML.

Note: If you use x:Name, the control will also be available as a field on the codebehind for your FrameworkElement, and you will be able to access it directly.

Upvotes: 5

Robert Levy
Robert Levy

Reputation: 29073

FrameworkElement.FindName will steer you in the right direction... http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.findname.aspx

Upvotes: 2

Related Questions