0

I have C# code linked to an xaml file that builds a GUI. In the GUI, when I click on an option, the photo appears, as I want. However, I can't figure out how to make it so the user has the option to close the photo. Here's my C# code:

public void help_click(object sender, RoutedEventArgs e)
{
     Image img;
     img = new Image();
     Uri diagram = new Uri(@"pack://application:,,,/PTDGUI;component/Content/Icons/controlmap.png", UriKind.RelativeOrAbsolute);
     img.Source = new BitmapImage(diagram);
     canvasSpace.Children.Add(img);            
}
Question_Guy
  • 323
  • 2
  • 3
  • 17
  • Make img as class variable... then remove it from the canvas. Like canvasSpace.Children.Remove(img); – Ayyappan Subramanian May 15 '15 at 02:20
  • Two questions, is this going to be the only image added to your Canvas and if not are there going to be multiple images on the Canvas at one time? – Mark Hall May 15 '15 at 14:05
  • @Mark Hall At times it'll be the only image on the canvas. At other times there would be multiple. – Question_Guy May 16 '15 at 03:49
  • The way you are creating them in your click event makes you have to search canvasSpace.Children collection for them. If you know how many you have I would make a class level variable like Lighswitch suggested. That will make it cleaner IMHO – Mark Hall May 16 '15 at 03:55

2 Answers2

0

Image is not based on Control. You may require to customize for this behavior by either writing custom control or trapping any other control events to close this image.

This discsussion(stackoverflow) explains both the options I've mentioned, should help in understand and implementing what you rquired.

Community
  • 1
  • 1
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
0

you can use the same help button for both, initially button will display "Help", When user clicks help, load the image and change the button text to "Close" and toggle it back to "Help" when users clicks again.

if(button.Text ="Help") { ---load image button.Text = "Close"; } else { --Clear image button.Text ="Help" }

you may also use bool flag instead of checking text.

Ravi
  • 1
  • 3