CommunityToolkit / MVVM-Samples

Sample repo for MVVM package
Other
1.15k stars 224 forks source link

Update property when another changes #132

Closed MattePozzy closed 7 months ago

MattePozzy commented 7 months ago

Hi, in a old XF application I have some properties declare like this one: public bool SpeseWOButtonVisible { get { return SpeseWOVisible || BtutteSezioniChiuse; } } How can I bring them into .MAUI and turn them into an ObservableProperty?

I used this code

public bool SpeseWOButtonVisible { get { return SpeseWOVisible || BtutteSezioniChiuse; } }
[ObservableProperty]
private bool _speseWOVisible;
private bool BtutteSezioniChiuse = true;

but when I update from the ViewModel SpeseWOVisible or BtutteSezioniChiuse, SpeseWOButtonVisible is not updated and consequently the UI does not update as well.

How can I solve this?

Thank you.

DaveInCaz commented 7 months ago

Have you looked at https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/observableproperty#notifying-dependent-properties ?

I think it is meant for situations like this.

MattePozzy commented 7 months ago

Have you looked at https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/observableproperty#notifying-dependent-properties ?

I think it is meant for situations like this.

Hi @DaveInCaz, thank you for your reply but unfortunately I have already looked at that link but could not apply it to my case. I tried using NotifyPropertyChangedFor but it still doesn't update the UI.

Olaf-R commented 7 months ago

I'm not sure as to whether I oversee the obvious or misunderstand the problem, but here's two ways to react to changes to an Observable property in the MVVM toolkit:


[ObservableProperty]
[NotifyPropertyChangedFor(nameof(OtherProperty))]
private bool originalProperty;

partial void OnOriginalPropertyChanged(bool value)
{
    // do something ...
}