In WPF, I have a property with only a get{}. The value is coming from the return of method. NotifyPropertyChanged is often used within a property set{}. It can then notify the UI and the updated value is displayed. But with a get{} only, there isn’t a way of detecting if a new or different value is available since you have to execute the method to check.
Is there some way to update the UI without having to keep a local value that contains the last value from the method, in which case a compare would need to be done?
The problem boils down to this: What is changing the property? The method that changes the property should call your
NotifyPropertyChangedwith the name of the property, which will, in turn, cause WPF to refetch the value and update the user interface.WPF doesn’t care where the
PropertyChangedevent gets raised – it just listens for it, and refreshes the binding as needed.Edit in response to comment:
It sounds like this may be the result of performing some operation upon your Model class. In this case, it’s common that you know that one or more properties may change, but not necessarily which ones. If your Model implements
INotifyPropertyChanged, you can subscribe to that and “bubble up” the notifications to the UI via yourNotifyPropertyChangedmethod. If it does not, however, there is another option.If you know that one or more properties may change, you can raise a
PropertyChangedevent withstring.Emptyas thePropertyNamein the EventArgs. This will cause all bound properties to get refreshed by WPF. This is likely just a matter of putting something like this in your code:I wouldn’t recommend doing this everywhere, however, as it does add overhead – but it’s often the cleanest solution if you don’t know which properties will change when you perform an operation.