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")); } } 
6
  • 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? Commented Dec 14, 2015 at 12:06
  • Yes, I checked that it is the object in the ObserableCollection defined in the itemsViewSource but the change doesn't appear. Commented Dec 14, 2015 at 13:55
  • try ti use DynamicResource instead of StaticResource Commented Dec 14, 2015 at 14:06
  • Do you have the value of downloaded in this case? And can you add it to your question (or as comment)? Commented Dec 14, 2015 at 14:07
  • downloaded = "/Assets/available.png" Commented Dec 14, 2015 at 14:12

5 Answers 5

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"); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thans for your answer, as I said, the notifyPropertyChanged is called but the image doesnt change.
@AzirisMorora - You should change the binding to UriSource and use Convertor to convert the relative source to BitMap image. You can try this link to implement converter.
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")); } } } 

Comments

0

Add UpdateSourceTrigger=PropertyChanged to you binding.

1 Comment

I just did, unfortunately, the image is still not updated.
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)

2 Comments

Thank you for the answer but it didn't solve my problem.
@AzirisMorora - Added another possible solution
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"/> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.