Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4123554
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:37:34+00:00 2026-05-20T23:37:34+00:00

I want to be able to refer to a WPF element in C# via

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:37:34+00:00Added an answer on May 20, 2026 at 11:37 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don’t know if it’s possible, but I want to be able to refer
I want to create something like a recorder whichs tracks all actions of a
First question here. Please excuse noob errors. I want to be able to refer
basically I have a link like so: <a href=file.php?value=this&something=so&please=help>special link</a> And when I click
I would like to be able to use Full text search in Code First
I want to be able to have a large master array and refer to
function Foo(){ } Foo.prototype={ foo:'some text' ,bar:function(){ console.log('Want to be able to retrieve foo
I'm using LaTeX and BibTeX for an article, and I want to able to
Want to be able to provide a search interface for a collection of objects
I want to be able to take an image that i have already captured

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.