Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to add properties to my Model playing with my first MVVM app. Now I want to add a place to save specific data in a clean way, so I used a struct. But I am having issues to notify property changed, it does not have access to the method (An object reference is required for the non-static field) Can someone explain to me why this happens and inform me on a strategy that fit my needs?

Thanks!

public ObservableCollection<UserControl> TimerBars
{
    get { return _TimerBars; }
    set
    {
        _TimerBars = value;
        OnPropertyChanged("TimerBars");
    }
}

public struct FBarWidth
{
    private int _Stopped;
    public int Stopped
    {
        get { return _Stopped; }
        set
        {
            _Stopped = value;
            OnPropertyChanged("Name"); //ERROR: An object reference is required for the non-static field
        }
    }

    private int _Running;
    //And more variables
}


private void OnPropertyChanged(string propertyName)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
public event PropertyChangedEventHandler PropertyChanged;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
252 views
Welcome To Ask or Share your Answers For Others

1 Answer

OnPropertyChanged needs to be defined in the scope that you wish to update properties on.

For that to work you'll have to implement the interface INotifyPropertyChanged.

And finally you have to provide the correct argument to the OnPropertyChanged method. In this example "Stopped"

public struct FBarWidth : INotifyPropertyChanged
{
    private int _Stopped;
    public int Stopped
    {
        get { return _Stopped; }
        set
        {
            _Stopped = value;
            OnPropertyChanged("Stopped");
        }
    }

    private int _Running;
    //And more variables

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

Edit: In your comment you mentioned that you've got a class sorounding the code you provided in your example.

That means you've nested a struct inside a class. Just because you've nested your struct, doesn't mean it inherits properties and methods from the outer class. You still need to implement INotifyPropertyChanged inside your struct and define the OnPropertyChanged method inside it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...