How can I change image visibility which is in mainwindow from another Window in WPF

Nehal Chaudhari 41 Reputation points
2022-01-21T06:09:06.79+00:00

I am changing the visibility of image which is in MainWindow by using
167048-screenshot-2022-01-21-113652.png167038-screenshot-2022-01-21-113512.png
MainWindow mainWindow = new MainWindow();
mainWindow.NoFiles.Visibility = Visibility.Hidden;

where NoFiles is my image name;

and I have write this code in another window Button handler but its not working.

After Clicking on Download button I want to remve NoFIleImage which is in my MainWindow.

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2022-01-21T07:07:02.8+00:00

    Hi Nehal,
    you can use Binding to Property in DataContext. Changing the value of this property and notify this the Visibility will be changed, For demonstration try following demo:

    XAML MainWindow:

    <Window x:Class="WpfApp1.Window002"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp002"  
            mc:Ignorable="d"  
            Title="Window002" Height="450" Width="800">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
      <Grid>  
        <Grid.RowDefinitions>  
          <RowDefinition Height="auto"/>  
          <RowDefinition/>  
        </Grid.RowDefinitions>  
        <Grid Grid.Row="0" Background="LightGreen">  
          <Button Content="Add Url" Command="{Binding}" CommandParameter="AddUrl"  HorizontalAlignment="Left" Width="100" Margin="5"/>  
        </Grid>  
        <Grid Grid.Row="1">  
          <Image Source="/Images/no_files.png" Width="100" Height="100" Visibility="{Binding VisibilityOfPicture}"/>  
        </Grid>  
      </Grid>  
    </Window>  
    

    XAML secondary Window:

    <Window x:Class="WpfApp1.Window002A"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp1"  
            mc:Ignorable="d"  
            Title="Add URL" Height="140" Width="500">  
      <Grid>  
        <Grid.ColumnDefinitions>  
          <ColumnDefinition Width="100"/>  
          <ColumnDefinition/>  
        </Grid.ColumnDefinitions>  
        <Grid.RowDefinitions>  
          <RowDefinition Height="50"/>  
          <RowDefinition/>  
        </Grid.RowDefinitions>  
        <Label Grid.Row="0" Grid.Column="0" Content="Add URL:" HorizontalAlignment="Center" VerticalAlignment="Center"/>  
        <TextBox Grid.Row="0" Grid.Column="2" Height="30" Margin="10"/>  
        <StackPanel Grid.Row="1" Grid.Column="2"  Orientation="Horizontal">  
          <Button Content="Check" Margin="10" Command="{Binding}" CommandParameter="Check" Width="80"/>  
          <Button Content="Download" Margin="10" Command="{Binding}" CommandParameter="Download" Width="80"/>  
        </StackPanel>  
      </Grid>  
    </Window>  
    

    and ViewModel class:

    using System;  
    using System.ComponentModel;  
    using System.Runtime.CompilerServices;  
    using System.Windows;  
    using System.Windows.Input;  
    using WpfApp1;  
      
    namespace WpfApp002  
    {  
      public class ViewModel : ICommand, INotifyPropertyChanged  
      {  
        public Visibility VisibilityOfPicture { get; set; } = Visibility.Visible;  
      
        public void Execute(object? parameter)  
        {  
          if (parameter == null) return;  
          switch (parameter.ToString())  
          {  
            case "AddUrl":  
              Window002A wnd = new Window002A();  
              wnd.DataContext = this;  
              wnd.ShowDialog();  
              break;  
            case "Check":  
              VisibilityOfPicture = Visibility.Hidden;  
              OnPropertyChanged(nameof(VisibilityOfPicture));  
              break;  
            case "Download":  
      
              break;  
            default:  
              break;  
          }  
        }   
        public event EventHandler? CanExecuteChanged;  
        public bool CanExecute(object? parameter) => true;  
      
        public event PropertyChangedEventHandler PropertyChanged;  
        private void OnPropertyChanged([CallerMemberName] string propName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));  
      }  
    }  
    

    Result:

    167114-x.gif

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.