-1

I have an image in wpf application and bind its Source property to System.Windows.Controls.Image in view model but it is not working. But when i bind its Source property to a BitmapImage, it is working. Is there any way to bind Source property as System.Windows.Controls.Image ?

Xaml Part

  <Image x:Name="ShipMainImage" Source="{Binding MainImageSource}" />

View model Part

 public class StabilityVM : INotifyPropertyChanged {
    private System.Windows.Controls.Image mainImageSource;

    public System.Windows.Controls.Image MainImageSource
    {
        get { return mainImageSource; }
        set {
            mainImageSource = value;
            OnPropertyChanged(nameof(MainImageSource)); }
    }

 protected virtual void OnPropertyChanged(string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

   public event PropertyChangedEventHandler PropertyChanged;

}

nihasmata
  • 652
  • 1
  • 8
  • 28

1 Answers1

1

Do not use System.Windows.Controls.Image as type of that property. It is a UI element type that is used in a view, but not in a view model.

Declare the property as System.Windows.Media.ImageSource - the type of the Source property of the Image element:

private ImageSource mainImageSource;

public ImageSource MainImageSource
{
    get { return mainImageSource; }
    set
    {
        mainImageSource = value;
        OnPropertyChanged(nameof(MainImageSource));
    }
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Thank you @Clemens for answer. My problem is that why binding of xaml image Source property to System.Windows.Controls.Image is not working. Only binding of Source property to BitmapImage is working. I updated my question please have a look. – nihasmata Jul 12 '20 at 12:01