简体   繁体   中英

Updating Viewmodel from Event - PropertyChangedEventHandler is null?

I have a property in my WPF app that I am trying to bind to the UI. When I update the property directly inside the viewmodel it works as I expect it to. However, when I try to update this property from inside another class using an Event, the binding doesn't seem to work. Under closer inspection I think this is happening because PropertyChangedEventHandler is null when this happens.

My property inside the ViewModel:

public int BeatNumber
        {
            get
            {
                return beatNumber;
            }
            set
            {
                if (beatNumber != value)
                {
                    this.beatNumber = value;
                    RaisePropertyChanged(() => BeatNumber);
                }
            }
        }

Event inside ViewModel:

public event EventHandler GetHtmlDone = delegate { };


public void GetHTML(string url)
{
    BeatNumber++;
}

Notification Object:

public class NotificationObject : INotifyPropertyChanged
    {
        protected void RaisePropertyChanged<T>(Expression<Func<T>> action)
        {
            var propertyName = GetPropertyName(action);
            RaisePropertyChanged(propertyName);
        }

        private static string GetPropertyName<T>(Expression<Func<T>> action)
        {
            var expression = (MemberExpression)action.Body;
            var propertyName = expression.Member.Name;
            return propertyName;
        }

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



        public event PropertyChangedEventHandler PropertyChanged;
    }

How do I get this binding to update when my event is called from another class?

Solve it by using Commands instead of calling methods from Events.

See ICommand interface for more information

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