CommunityToolkit / MVVM-Samples

Sample repo for MVVM package
Other
1.11k stars 222 forks source link

OnChangedInvokeMethodAttribute? #128

Open yangf85 opened 9 months ago

yangf85 commented 9 months ago

Does the Community Toolkit have a feature that allows calling a method when a property changes, similar to Fody? like this. feff5e4a1c030f2f139b6939c66aacff thanks

yangf85 commented 9 months ago

I know that partial methods can be used to implement a method call, but now there are multiple properties, and I don’t want to use the PropertyChanged += PropertyChanged event to handle them. like this: image

echoix commented 9 months ago

If SearchValue is an ObservableProperty, you can do:

[ObservableProperty]
private string? searchValue;

partial void OnSearchValueChanged(string? value) 
{
    SearchGame();
}

There are at least 4 partial methods available that can be used for every observable property. Before the change and after the change. Two of them have the value before and after.

If SearchGame() is another property to simply get notified that it got changed (it doesn't have to be an observable property), like a "fullName" property that should be updated when either observable property "firstName" or "lastName" gets changed, you can use the NotifyPropertyChangedFor attribute, with the name of the property to get notified it should be updated too.