There are several approaches to solving your problem. Consider one of them:
1. Add images to resources (It is convenient to create an Images folder).
2. In your VM create the IsFirstImage property, which will be responsible for which image will be displayed in your Image
private bool isFirstImage; public bool IsFirstImage { get { return isFirstImage; } set { isFirstImage = value; OnPropertyChanged("IsFirstImage"); } }
3. Create a command that is responsible for handling the button press:
private ICommand clickCommand; public ICommand ClickCommand { get { return clickCommand; } set { clickCommand = value; } }
4. It remains to write a function that will change the value to the opposite:
private void changeImage() { IsFirstImage = !IsFirstImage; }
5. VM Designer:
public MainViewModel() { clickCommand = new RelayCommand(_ => changeImage()); }
6. Further describe the markup:
<Window.DataContext> <local:MainViewModel /> </Window.DataContext> <Window.Resources> <Style x:Key="image"> <Style.Triggers> <DataTrigger Binding="{Binding IsFirstImage}" Value="True"> <Setter Property="Image.Source" Value="Images/Koala.jpg" /> </DataTrigger> <DataTrigger Binding="{Binding IsFirstImage}" Value="False"> <Setter Property="Image.Source" Value="Images/Penguins.jpg" /> </DataTrigger> </Style.Triggers> </Style> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Button Grid.Row="0" HorizontalAlignment="Right" Command="{Binding ClickCommand}" Content="Click" /> <Image Grid.Row="1" Style="{StaticResource ResourceKey=image}" /> </Grid>
The data trigger allows you to set the displayed image depending on the value of your property.
The code for RelayCommand and the implementation of the INotifyPropertyChanged interface INotifyPropertyChanged not INotifyPropertyChanged in order not to clutter up the response.
The result is as follows:
