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: