I wanted to make the picture change when I pressed the button. I wanted to use code from windows forms :

private void Play_btn_Click(object sender, EventArgs e) { if (mediaElement.playState == WMPLib.WMPPlayState.wmppsPlaying) { mediaElement.Ctlcontrols.pause(); Play_btn.BackgroundImage = Properties.Resources._11; } else { mediaElement.Ctlcontrols.play(); Play_btn.BackgroundImage = Properties.Resources._112; } } 

But in WPF this is not possible. Tell me how to do it.

    2 answers 2

    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:

    enter image description here

      1. Use the Image control.
      2. Image should receive a new Bitmap through the ImageSource property.
      3. Make sure that the images you use have a role in the project as a Resource.