简体   繁体   中英

[WPF C#]PropertyChanged function called but update does not happen

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"));
        }
    }

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");
    }
}   

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"));
        }
    }
}

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)

将UpdateSourceTrigger = PropertyChanged添加到您的绑定中。

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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM