Search code examples
c#windows-phone-8visual-studio-2013visual-studio-2015

binding data to observable collection in windows phone


This is the class am using. Am Adding data by calling the constructor. But the value in image is not copied IMG variable. All other data is binded. If I change IMG to normal get; set;, then also the binding works correctly. When setting IMG the value is not passed to IMG. I dont know the reason. That is the problem. Please help with this.

public class ingre : INotifyPropertyChanged
{
    //private string ing;
    private string img { get; set; }
    //private string si;

    public event PropertyChangedEventHandler PropertyChanged;

    public ingre(string image, string name, string shopitem)
    {
        ING = name;
        //string image,
        IMG = image;
        SI = shopitem;

    }


    public string ING
    {
        set;
        get;
    }


    public string IMG 
    {
        get { return img; }
        set
        {
            img = IMG;
            NotifyPropertyChanged("IMG");
        }

    }

    public string SI 
    {
        set;
        get;
    }

    public override string ToString()
    {
        return ING + IMG;
    }

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

Solution

  • Try this

    this is your IMG property

    public string IMG {
    
        get { return img; }
        set
        {
           if(value != img)
           {
               img = value;
            NotifyPropertyChanged();
           }
    
        }
    

    This is your NotifyPropertyChanged Method

      private void NotifyPropertyChanged(
                                          [System.Runtime.CompilerServices.CallerMemberName]
                                          string   Property = null) 
    
    {  
       PropertyChanged(this, new PropertyChangedEventArgs(Property));        
    }