0

I have a string, imgchng, and twenty images, image1, image2, image3,etc.

The value of imgchng is always the name of one of the images.

How would I set the value of the the current image that imgchng is referring to?

For instance, the user sets the value of imgchng as image12. How would I tell image12's source to change?

imgchng.Source = (source goes here); doesn't work, because that would set the property of the string, not the image.

I know how to set the source of the image, just not how to set the source of whatever image the string is referring to.

My intention is to avoid a humongous if statement that would be over 1000 lines long, like the example one below:

        if (textBlock2.Text == "First User Selection")
        {
            if (imgchng == "image1")
            {
                BitmapImage bmp = new BitmapImage(new Uri("/Images/FirstImg.png"));
                image1.Source = bmp;
            }
            else if (imgchng == "image2")
            {
                BitmapImage bmp = new BitmapImage(new Uri("/Images/FirstImg.png"));
                image2.Source = bmp;
            }
            //Continue this for all 20 images
        }
        else if (textBlock2.Text == "Second User Selection")
        {
            if (imgchng == "image1")
            {
                BitmapImage bmp = new BitmapImage(new Uri("/Images/SecondImg.png"));
                image1.Source = bmp;
            }
            else if (imgchng == "image2")
            {
                BitmapImage bmp = new BitmapImage(new Uri("/Images/SecondImg.png"));
                image2.Source = bmp;
            }
            //Continue this for all 20 images
        }
        else if (textBlock2.Text == "Third User Selection")
        {
            if (imgchng == "image1")
            {
                BitmapImage bmp = new BitmapImage(new Uri("/Images/ThirdImg.png"));
                image1.Source = bmp;
            }
            else if (imgchng == "image2")
            {
                BitmapImage bmp = new BitmapImage(new Uri("/Images/ThirdImg.png"));
                image2.Source = bmp;
            }
            //Continue this for all 20 images
        }
        else if (textBlock2.Text == "Fourth User Selection")
        {
            if (imgchng == "image1")
            {
                BitmapImage bmp = new BitmapImage(new Uri("/Images/FourthImg.png"));
                image1.Source = bmp;
            }
            else if (imgchng == "image2")
            {
                BitmapImage bmp = new BitmapImage(new Uri("/Images/FourthImg.png"));
                image2.Source = bmp;
            }
            //Continue this for all 20 images
        }

Basically what I'm trying to do is, as @ctacke said, given the string 'image1', how do I get the instance of the control named 'image1'?

4
  • what do you mean by image12's source.. isn't that the name of the image itself..? Commented May 28, 2011 at 3:23
  • The source of image12 is a small 50*50 image. I know how to set the background of the image, just not how to decide which image to change without using a humongous if statement. Commented May 28, 2011 at 14:47
  • 2
    So is the question here "Given the string 'image1', how do I get the instance of the control named 'image1'?" Commented May 28, 2011 at 16:28
  • @ctacke: Yeah, that's basically what I want to do. Commented May 28, 2011 at 16:46

2 Answers 2

2

Declare a BitmapImage array of 20 images. Bind that to the UI. Take the user selection as an integer. Access the image from array using index (obviously user input -1).Change the source of that image. Does this resolve your problem?

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that solves it. Sort of tedious to do all of the BitmapImage img10 = new BitmapImage();s (I had to do this on 5 different XAML pages), but it's still a shorter solution. Thanks.
-1


To set the source of the image you have to do the following.


BitmapImage bmp=new BitmapImage(new Uri("your image name will go here"));
image.Source=bmp;

Hope this helps.

1 Comment

No, what I want to do is detect which image to change based on the value of imgchng.For instance, if the value of ingchng is "image1", then I want to change the value of image1. If the value of imgchng is "image19", then I want to change the background of image19. I know how to set the background of the image- just not how to decide which image to change without using a humongous if statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.