Dante1986
Dante1986

Reputation: 59939

Image name as string, use it to make image visible WPF with C#

I have an image name as a string. The real imagename on the form is called "image". So i get something like this:

    image.Visibility = Visibility.Hidden;

string imageName = "image";
// need something here to make it usable...

changedImageName.Visibility = Visibility.Visible;

Now, a string can not be used in combination with the Visibility property. I cant really find what i must make the string to, to make it usable for the visibility property. If i see this page: http://msdn.microsoft.com/en-us/library/system.windows.visibility.aspx Do I understand correct that I make it a "enum" ? And if yes, how do I get a string to that property?

EDIT 1

I see I have not been explaining it proper enough. I forgot to mention I am using a WPF form. on this form, I have put an image.

In the initialize part, the image get set to hidden. so for example the imagename I named "Image" so I use image.Visibility = Visibility.Hidden;

later on in my code, I want to make the image visible again, depending on what the user does. so, instead if just using the name to get the image visible again, I want to use a string. this string is looking exactly as the name of the image. but i cant use the string in combination with the Visibility function. but i cant find anywhere what i must make this string to, to be able to use that visibility option on it. hope i explained a bit better now :).

Later on, i will have multiple images on the WPF window. So the key is that i will use the string, that is corresponding with the name of the image. Depending on what the user has input into the string, some image will or will not show.

EDIT 2

If you have:

String theName = ImageName.name

you can get the name of the image into a string, so you can do stuff with it. i am looking for a way to do the exact opposite of this. So i want to go from a string, to that name, so after that i can use this to control the image again.

Edit 3

some example:

        private void theButton_Click(object sender, RoutedEventArgs e)
        {

            //get the Name property of the button
            Button b = sender as Button;
            string s = b.Name;

            MessageBox.Show("this is the name of the clicked button: " + s);

            //the name of the image to unhide, is the exact same as the button, only with IM in front so:
            string IM = "IM";
            IM += s;

            MessageBox.Show("this string, is now indentical to the name of the image i want to unhide, so this string now looks like: " + IM );

            // now, this wont work, because i cant use a string for this, although the string value looks exactly like the image .name property
            // so string IM = IMtheButton
            // the image to unhide is named: IMtheButton.name
            IM.Visibility = Visibility.Visible;
}

Upvotes: 0

Views: 2685

Answers (3)

Anders
Anders

Reputation: 8577

The code I was looking for:

  object item = FindName(IM);
  Image test1 = (Image)item;
  test1.Visibility = Visibility.Visible;

Upvotes: 0

Gustavo F
Gustavo F

Reputation: 2206

looks like you're using WPF, so you can create a boolean to visibility converter and use it with a boolean (and create a method that receives string if necessary) and just use:

<ContentControl Visibility="{Binding Path=IsControlVisible, Converter={StaticResource BooleanToVisibilityConverter}}"></ContentControl>

or any other converter...

check this links: http://bembengarifin.wordpress.com/2009/08/12/setting-visibility-of-wpf-control-through-binding/

http://andu-goes-west.blogspot.com/2009/05/wpf-boolean-to-visibility-converter.html

http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter.aspx

EDIT 1:

so then you will have to iterate over the images and check if your string is equals to name of the Image class.

something like this (not tested):

foreach (Image img in container.Items)
{
    if img.Name == yourMagicallyString;
    {
        img.Visibility = Visibility.Visible;
    }
else 
    {
        img.Visibility = Visibility.Hidden;
    }
}

Upvotes: 1

Chris Haas
Chris Haas

Reputation: 55427

If I understand correctly, you are trying to find a control based on the name or ID of the control. If so, try this:

Control changedImage = this.Controls.Find("image", false)[0];

Depending on what you are targeting and what version you might need to tweak a little

EDIT Updated per @Alexander Galkin comments about Find returning an array. There should definitely be some checking and whatnot but I'm leaving that up to the OP.

EDIT 2 For finding a control by name in WPF see this post.

Upvotes: 1

Related Questions