Raise PropertyChanged

Jun 11, 2011 at 8:01 PM

Hello Mr Horrik! I have a model that do not implement INotifyPropertyChanged with properties, let say, A and B. In a viewmodel I want to have property C that should return result of (A-B) subtraction. I also have method D that modify A and B from another thread. How could I make UI reflect changes in C? In a mvvm framework i used previously I just call RaisePropertyChange("C") in method D. What is the best way to do this with Catel? Thanks in advance!

Coordinator
Jun 14, 2011 at 9:19 AM

Hi InsederHSE,

Catel fully supports this as well. Just make a call to OnPropertyChanged() with either the registered property (very typesafe) or the string overload (which you need to do in case of a custom non-registered property).

Nov 27, 2011 at 6:22 PM
GeertvanHorrik wrote:

Hi InsederHSE,

Catel fully supports this as well. Just make a call to OnPropertyChanged() with either the registered property (very typesafe) or the string overload (which you need to do in case of a custom non-registered property).

Should I call RaisePropertyChanged()? It accepts 2 parameters. None of them is the registered property...

Coordinator
Nov 27, 2011 at 6:25 PM

If it is an automatic registered property, the PropertyChanged event is already called for the property. If not, you can do this:

RaisePropertyChanged("MyProperty");

or

RaisePropertyChanged(() => MyProperty);

Nov 27, 2011 at 6:42 PM

It is an automatic registered property. Problem is that the model does not raise prop changed event. When I call RaisePropertyChanged("TotalWorkingTime) I get

10:40:44:550 => [ERROR] [Catel.Reflection.PropertyHelper] Cannot write property OptionQuoter.Models.SingleOptionQuotingStrategy.'TotalWorkingTime'
10:40:44:597 => [WARNING] [Catel.Data.ObservableObject] Failed to set property 'TotalWorkingTime' on model type 'SingleOptionQuotingStrategy'

(setter is private)

Coordinator
Nov 27, 2011 at 7:03 PM

If a property is read-only, why use the ModelToViewModel attribute? You should call RaisePropertyChanged on the model (but consider using either ObservableObject or DataObjectBase for your models if they don't implement INotifyPropertyChanged). Can you show the code of the model and view model (just the relevant parts)?

Nov 27, 2011 at 7:32 PM

I've just removed the ModelToViewModel attribute and it works =) Thank you!

Nov 27, 2011 at 8:48 PM

So if a property is read-only, there is no need to register the property?

Coordinator
Nov 28, 2011 at 4:31 AM

It is best to register the property because you still need the INotifyPropertyChanged for it, right? I will see if I can implement a mode property for the ViewModelToModelMapping:

TwoWay
OneWay
OneWayToSource