0

I'm trying to change an image source by clicking on something.

my .xaml

<Canvas Name="img_Canvas" Grid.IsSharedSizeScope="True">
        <Image Source="{Binding ImageType_Source}" Name="autoImage" Margin="5"/>

my .cs

private string _ImageType_Source;
        public string ImageType_Source
        {
            get { return _ImageType_Source; }
            set { 
                _ImageType_Source = value;
                OnPropertyChanged("ImageType_Source");
            }
        }

        public exImage()
        {
            InitializeComponent();
            DataContext = this;

So when I click on e.g Ellipse, I want to change the image.

private void Ellipse_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ImageType_Source = "/ANKE_Test;component/imagesFolder/Bild_Getriebe.jpg";
            string test = autoImage.Source.ToString();

After changing the source the PropertyChangedEvent is fired with PropertyChanged = null but with the new source. Unfortunately the source in the image is still the old one and is not changing.

4
  • if PropertyChanged == null that implies that nothing is listening to the event so the UI can't react and change. Does your .cs implement INotifyPropertyChanged? Commented Nov 17, 2015 at 14:07
  • yes it does implement INotifyPropertyChanged Commented Nov 17, 2015 at 14:10
  • Shouldn't the code be if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(name)); rather than OnPropertyChanged(name);. You're not firing the event. Commented Nov 17, 2015 at 14:12
  • Thank you, totally forgotten !!! Commented Nov 17, 2015 at 14:15

1 Answer 1

1

When working with INotifyPropertyChanged I usually make my own NotifyPropertyChanged method like this:

private void NotifyPropertyChanged(String info) {
        if (PropertyChanged != null) {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

Did you do this to with your OnPropertyChanged method?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.