lolveley
lolveley

Reputation: 1719

OnPropertyChanged does not trigger getter of getter only property

My goal is to trigger GUI changes of a string property (TotalPrice), that only has a getter, of a realm object (Group), its calculus involving properties of related realm objects.

here are some relevant elements of the expected mechanism:

<editors:NumericEdit x:Name="QuantityInGroup" CornerRadius="8" LabelText="Quantité (groupe)" Grid.Row="1" Grid.Column="3" TextColor="{StaticResource Gray500}" TextFontSize="14" BoxHeight="40" HeightRequest="50" BoxPadding="10,10,10,5" DisplayFormat="0.00 ;(0.00 )" Value="{Binding AssociationInGroup.Quantity}"> <---

Here is TotalPrice:

[Ignored]
public string TotalPrice
{
    get
    {
        if (Associations.Count == 0
            || Associations.Any(asso => asso.ProductPricing is null))
            return null;
        var total = Associations.Sum(asso =>
            asso.ProductPricing!.IsPriced 
                ? asso.ProductPricing.UnitPrice * asso.Quantity <--
                : 0.0);
        return total.ToString("C", CultureInfo.GetCultureInfo("fr-FR"));
    }
}

in the ViewModel:

[RelayCommand]
public async Task RemoveAssociationFromGroup()
{
    SelectedGroup.ZePropertyChanged("TotalPrice");
}

which calls in the Group class:

public void ZePropertyChanged(string name)
{
    OnPropertyChanged(name);
}

Actual behavior: ZePropertyChanged is called, but the getter is not called, and hence the UI is not refreshed. If I exit and enter again in the page, the correct TotalPrice is displayed.

I could add an ObservableProperty to the ViewModel, and calculate TotalPrice manually, but I can't let such an error persist.

I can't use ObservableProperties in the RealmObject because it's a RealmObject already implementing INotifyPropertyChanged.

SelectedGroup does contain in a property (Associations) the Association whose Quantity has changed.

thank you

Upvotes: 0

Views: 94

Answers (1)

Nikola Irinchev
Nikola Irinchev

Reputation: 2186

OnPropertyChanged is a method invoked by Realm to notify you a property has changed - invoking it will not raise the event. Instead, you should use RaisePropertyChanged from within your model:

public void ZePropertyChanged(string name)
{
    RaisePropertyChanged(name);
}

Upvotes: 1

Related Questions