1

I have a small problem with my C# code and binding a property.

Here I have the following xaml:

<Image Source="{Binding downloaded, Source={StaticResource itemsViewSource}}" Width="20" Height="20" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Top"/>

And there is the code I'm trying to make working:

class Ressource : INotifyPropertyChanged
{
    public String downloaded        { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        Debug.WriteLine("Property changed.");
        PropertyChanged(this, new PropertyChangedEventArgs(info));
    }

}

My problem is that the NotifyPropertyChanged function is called (the debug appears), the string content is changed but I don't see my image appear.

Does anyone have a solution to this.

Thanks!

EDIT: After multiple useful answers but no change appearing even if the propertyChanged function is called,I'm starting to wonder if maybe changing the path of the image source is really possible.

Can the image be updated when the path is changed?

Here is the code after the changes suggested:

    public class Ressource : INotifyPropertyChanged
    {
    public String downloaded
    {
        get
        {
            return _downloaded;
        }
        set
        {
            _downloaded = value;
            if (PropertyChanged != null)
                PropertyChanged(this,new PropertyChangedEventArgs("downloaded"));
        }
    }
Pierre P.
  • 890
  • 1
  • 18
  • 41
  • Are you 100% sure that the instance where you are changing the `downloaded` property is the one that is bound? The resource with key `itemsViewSource`? Or is it another instance you created somewhere else? – Jcl Dec 14 '15 at 12:06
  • Yes, I checked that it is the object in the ObserableCollection defined in the itemsViewSource but the change doesn't appear. – Pierre P. Dec 14 '15 at 13:55
  • try ti use `DynamicResource` instead of `StaticResource` – StepUp Dec 14 '15 at 14:06
  • Do you have the value of `downloaded` in this case? And can you add it to your question (or as comment)? – bkardol Dec 14 '15 at 14:07
  • downloaded = "/Assets/available.png" – Pierre P. Dec 14 '15 at 14:12
  • Just for anyone having the same symptom but **do have the binding** in place - if you are **setting the bound property directly in the code, the binding will be lost**. To get around this, use `TwoWay` binding. – spacer Sep 22 '17 at 23:22

5 Answers5

2

You should change the property declaration so that view can be notified what source property is changed in View Model and update the control for notified property's value.

private String _downloaded;
public String downloaded 
{
    get  
    {
      return _downloaded;       
    }  
    set
    {
       _downloaded = value;
       NotifyPropertyChanged("downloaded");
    }
}   
user1672994
  • 10,509
  • 1
  • 19
  • 32
  • Thans for your answer, as I said, the notifyPropertyChanged is called but the image doesnt change. – Pierre P. Dec 14 '15 at 13:56
  • @AzirisMorora - You should change the binding to UriSource and use Convertor to convert the relative source to BitMap image. You can try [this](http://stackoverflow.com/questions/20586/image-urisource-and-data-binding) link to implement converter. – user1672994 Dec 14 '15 at 15:15
0

The C# View Model Source Code is

class Ressource : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private String _downloaded;
    public string downloaded
    {
        get { return _downloaded; }
        set
        {
            _downloaded= value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("downloaded"));
        }
    }
}
B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
0

You can only apply databinding when the accessibility level is public. The default accessibility level of a class is internal. You haven't applied an accessibility level, so your class Ressource is internal. Make it public and it should work.

public class Ressource : INotifyPropertyChanged

UPDATE 1:

If your image has been set to build action Resource you can try this string: "/AssemblyName;component/Assets/available.png".

OR for .Net Framework 4.5:

"pack://application:,,,/AssemblyName;component/Assets/available.png"

Replace AssemblyName with your assembly name (you can use Assembly.GetExecutingAssembly().GetName().Name to get your assembly name dynamically)

bkardol
  • 1,258
  • 1
  • 20
  • 32
0

Add UpdateSourceTrigger=PropertyChanged to you binding.

Jaster
  • 8,255
  • 3
  • 34
  • 60
0

Okay so that was a rookie mistake but I prefere showing what was the problem since I found no topic explaining what could have been the problem. However I found it myself so... I guess no one really needs it. Anyway:

I have not been clear enough. In my example, I have the image source bound to the downloaded field in the Resource class.

The problem was that the resource objects were contained in another class which did not implement INotifyPropertyChanged.

Once I did, everything works fine.

Thanks to everyone one who tried to help me, sorry for the noobness and lack of clarity.

Code following if anyone struggles with this:

Part of Resource :

    public class Ressource : INotifyPropertyChanged
{

    private String _downloaded;
    public String downloaded
    {
        get { return this._downloaded; }
        set
        {
            this._downloaded = value;
            raiseProperty("downloaded");
        }
    }
        public event PropertyChangedEventHandler PropertyChanged;

    private void raiseProperty(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

And the container :

   class personalSeance : INotifyPropertyChanged
{
        public ObservableCollection<Ressource> listRess { get; set; }
   public event PropertyChangedEventHandler PropertyChanged;

    private void raiseOnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

And the simplest binding ever:

<Image Source="{Binding downloaded}" Width="20" Height="20" Margin="3" HorizontalAlignment="Right" VerticalAlignment="Top"/>
Pierre P.
  • 890
  • 1
  • 18
  • 41