.Net Maui ObservableProperty does not always update view

171 views Asked by At

I am working on a Windows application in .NET Maui. I have an order form that users fill in by making selections from Pickers for the general order data. For the order lines the user selects/enters the information as needed and clicks a button to add the order line to the order. When a line is added the OrderTotal is calculated from all of the line totals. This all works fine, but when a user deletes a line the OrderTotal property is updated, but the Entry.Text on the UI does not get updated. The order rows are a Custom Control, and all the other bindings are working including the OrderTotal when a line is added, just not when one is deleted.

  • How I add a row -
public void AddOrderRow()
{
    if (PartPickerSelectedIndex != -1)
    {
        OrderRowModel orm = new()
        {
            Quantity = QuantityText,
            Unit = UnitList[UnitPickerSelectedIndex].UnitShortName,
            PartNumber = PartList[PartPickerSelectedIndex].PartNumber,
            Description = PartList[PartPickerSelectedIndex].PartDescription,
            UnitPrice = UnitPriceText,
            Per = PerText,
            Total = LineTotalPriceText
        };

        GlobalAssets.OrderRowList.Add(orm);

        CalculateOrder();

    }
}
  • How I delete a row -

        public void Delete(object obj)
        {
            int RowId = (int)(obj as int?);

            GlobalAssets.OrderRowList.RemoveAt(
                    GlobalAssets.OrderRowList.IndexOf(
                        GlobalAssets.OrderRowList.Where(
                            x => x.RowId == RowId).Single()));

            CalculateOrder();
        }


  • How I calculate OrderTotal -
    public void CalculateOrder()
    {

        decimal OrderTotal = 0;

        foreach (var item in GlobalAssets.OrderRowList)
        {
             OrderTotal += decimal.Parse(item.Total, NumberStyles.Currency);
        }

             OrderTotalText = OrderTotal.ToString("C2", CultureInfo.CurrentCulture);
    }
  • My GlobalAssets - (Holds My List of OrderRows)
    public partial class GlobalAssets : ObservableObject
    {
        #region ObservableCollection Properties
        public static ObservableCollection<OrderRowModel> OrderRowList { get; } = new();
        #endregion
    }
  • My Properties for the OrderTotal field -
       #region Order Total Properties
       [ObservableProperty]
       private string orderTotalLabelText;
       [ObservableProperty]
       private int orderTotalControlWidth;
       [ObservableProperty]
       private int orderTotalContentWidth;
       [ObservableProperty]
       private string orderTotalText;
       [ObservableProperty]
       private string orderTotalPlaceholderText;
       #endregion

  • My Xaml for OrderTotal field -
            <Grid ColumnDefinitions="Auto, 140"
                  RowDefinitions="8"
                  HorizontalOptions="End">
                <Label
                    Grid.Column="0"
                    Grid.Row="0"
                    Text="{Binding OrderTotalLabelText}"
                    TextColor="{Binding LabelColor}"
                    FontSize="{Binding LabelFontSize}"
                    HeightRequest="20"
                    Padding="35, 0, 0, 0"
                    BackgroundColor="{Binding BackgroundColor}"/>
                <Frame
                    Grid.Column="1"
                    Grid.Row="0"
                    BackgroundColor="{Binding BackgroundColor}"
                    CornerRadius="{Binding ControlCornerRadius}"
                    WidthRequest="{Binding OrderTotalControlWidth}"
                    HeightRequest="{Binding ControlHeight}"
                    BorderColor="{Binding BorderColor}"
                    Padding="20,1,20,1">
                    <Editor
                        Placeholder="{Binding OrderTotalPlaceholderText}"
                        PlaceholderColor="{Binding PlaceholderColor}"
                        Text="{Binding OrderTotalText, Mode=TwoWay}"
                        TextColor="{Binding ContentColor}"
                        BackgroundColor="{Binding BackgroundColor}"
                        WidthRequest="{Binding OrderTotalContentWidth}"
                        HeightRequest="{Binding ContentHeight}"
                        IsEnabled="{Binding IsOrderTotalEnabled}"
                        VerticalOptions="Start"
                        VerticalTextAlignment="Start"/>
                </Frame>
            </Grid>

I have tried to move all of the associated Properties to the GlobalAssets. I have tried to eliminate the CalculateOrder method and just duplicathe the code once in the add line and once in the delete line.

I have used Breakpoints all throughout and I know that the property itself (OrderTotalText) is updating, but it only updates the UI when I add a line, not when I delete a line.

I tried the suggestion from WPF Control binding does not always update UI and changed my binding to:

Text="{Binding OrderTotalText, UpdateSourceEventName=OnPropertyChanged, Mode=OneWay}"

Still no UI update for OrderToalText when deleting a line.

0

There are 0 answers