I am adding the button and image element using code. My wpf application is able to display an image stored on my project when I click the button. I want to hide the displayed image if I clicked the button again. How will I be able to achieve this if I only have one event handler for my button?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
grid_parent.RowDefinitions.Add(new RowDefinition { Height = new GridLength(150, GridUnitType.Pixel) });
grid_parent.RowDefinitions.Add(new RowDefinition { Height = new GridLength(150, GridUnitType.Auto)});
Button btn_submit = new Button { Content = "Submit" };
btn_submit.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
Grid.SetRow(btn_submit, 1);
btn_submit.Click += btn_submit_Click;
grid_parent.Children.Add(btn_submit);
}
void btn_submit_Click(object sender, RoutedEventArgs e)
{
image = new Image { Source = new BitmapImage(new Uri(@"/AddControlsThroughClass;component/images/julie.jpg", UriKind.Relative)) };
image.Stretch = Stretch.Uniform;
Grid.SetRow(image, 0);
grid_parent.Children.Add(image);
}
}