'Xamarin - ListView showing all property values but one

I have a Content Page in which I want to show an invoice, for that I have two listviews.

The first one contains the name of the customer, the date and the total cost.

The thing is to get the total cost I have used Shell navigation with parameters and it works fine just not in the listview.

So here is my ViewModel

  [QueryProperty(nameof(Total), "total")]
    public class InvoiceViewModel : BindableObject
    { 
        string _total;
        public string Total
        {
            get => _total;
            set
            {
                _total = Uri.UnescapeDataString(value ?? string.Empty);
                OnPropertyChanged();
            }
        }

        public InvoiceViewModel()
        {
            _oListOrders = new ObservableCollection<CrOrder>();
            GetListCart();
        }


        private ApiService _apiService = new ApiService();
        public List<Invoice> _oListCart = new List<Invoice>();
        public ObservableCollection<CrOrder> _oListOrders;

        public List<Invoice> ListCart
        {
            get => _oListCart;
            set
            {
                _oListCart = value;
                OnPropertyChanged();
            }
        }

        private async void GetListCart()
        {
            // call database here
            var customerId = Convert.ToInt32(CurrentPropertiesService.GetCustomer());

            var customer = await _apiService.GetCustomerById(customerId, CurrentPropertiesService.GetToken());

            _oListCart.Add(new Invoice
            {
                Name = customer.Data.Name,
                Date = DateTime.UtcNow,
                Total = Convert.ToDecimal(_total),
                ListOrders = await GetListOrders()
            });
        }

        private async Task<ObservableCollection<CrOrder>> GetListOrders()
        {
            // call database here
            var cartId = Convert.ToInt32(CurrentPropertiesService.GetCart());

            var orders = await _apiService.GetOrdersByCart(cartId, CurrentPropertiesService.GetToken());

            ObservableCollection<CrOrder> collection = new ObservableCollection<CrOrder>(orders.Data);

            return collection;

        }
    }

Here is what I have in my View

 <ContentPage.Content>
        
        <StackLayout BackgroundColor="#A7A7A7">
            <Label Text="{Binding Total}" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"></Label>
            <ListView x:Name="ListViewCart" ItemsSource="{Binding ListCart}" HasUnevenRows="True" IsPullToRefreshEnabled="False" >
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="model:Invoice">
                        <ViewCell>
                            <Frame HasShadow="True" Margin="2" BorderColor="Gray">
                                <Grid Margin="0" HorizontalOptions="FillAndExpand" VerticalOptions="Center" HeightRequest="150" RowSpacing="0" ColumnSpacing="0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="auto"/>
                                        <RowDefinition Height="auto"/>
                                        <RowDefinition Height="auto"/>
                                        <RowDefinition Height="auto"/>
                                        <RowDefinition Height="auto"/>
                                        <RowDefinition Height="auto"/>
                                        <RowDefinition Height="auto"/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="5*"/>
                                        <ColumnDefinition Width="3*"/>
                                        <ColumnDefinition Width="2*"/>
                                    </Grid.ColumnDefinitions>
                                    <Label Grid.Row="0" Grid.Column="0" Text="Nombre del Cliente:" VerticalOptions="Start"/>
                                    <Label Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding Name}" VerticalOptions="Start"/>

                                    <Label Grid.Row="1" Grid.Column="0" Text="Fecha Compra:" VerticalOptions="Start"/>
                                    <Label Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding Date}" VerticalOptions="Start"/>

                                    <Label Grid.Row="2" Grid.Column="0" Text="Monto Total:" VerticalOptions="Start"/>
                                    <Label Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding Total, StringFormat='{0:N2}€'}" VerticalOptions="Start"/>

                                    <Label Grid.Row="3" Grid.ColumnSpan="3" Text="Detalle Compra" VerticalOptions="Start"/>
                                    <BoxView Grid.Row="4" Grid.ColumnSpan="3" Color="Gray" HeightRequest="2" HorizontalOptions="Fill" />

                                    <Label Grid.Row="5" Grid.Column="0" Text="Nombre" VerticalOptions="Start" TextColor="#9C2424"/>
                                    <Label Grid.Row="5" Grid.Column="1" Text="Monto" VerticalOptions="Start" TextColor="#9C2424"/>
                                    <Label Grid.Row="5" Grid.Column="2" Text="Cantidad" VerticalOptions="Start" TextColor="#9C2424"/>

                                    <StackLayout Grid.Row="6" Grid.ColumnSpan="3" Margin="0" Padding="0">
                                        <ListView x:Name="ListViewOrders" ItemsSource="{Binding ListOrders}" HasUnevenRows="True" IsPullToRefreshEnabled="False" >
                                            <ListView.ItemTemplate>
                                                <DataTemplate x:DataType="model:CrOrder">
                                                    <ViewCell IsEnabled="False">
                                                        <Grid RowSpacing="0" ColumnSpacing="0" Margin="0" MinimumHeightRequest="50">
                                                            <Grid.ColumnDefinitions>
                                                                <ColumnDefinition Width="5*"/>
                                                                <ColumnDefinition Width="3*"/>
                                                                <ColumnDefinition Width="2*"/>
                                                            </Grid.ColumnDefinitions>
                                                            <Grid.RowDefinitions>
                                                                <RowDefinition/>
                                                            </Grid.RowDefinitions>
                                                            <Label Grid.Row="0" Grid.Column="0" Text="{Binding Reference}" TextColor="Black" VerticalOptions="Center" FontSize="12" />
                                                            <Label Grid.Row="0" Grid.Column="1" Text="{Binding Price, StringFormat='{0:N2}€'}" TextColor="Black" VerticalOptions="End" FontSize="12" />
                                                            <Label Grid.Row="0" Grid.Column="2" Text="{Binding Quantity}"  TextColor="Black" VerticalOptions="End" FontSize="12"/>
                                                        </Grid>

                                                    </ViewCell>
                                                </DataTemplate>
                                            </ListView.ItemTemplate>
                                        </ListView>...

And on my ContentPage I don't have anything

 [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class InvoicePage : ContentPage
    {

        public InvoicePage()
        {
            InitializeComponent();

            //BindingContext = new InvoiceViewModel();
        }
    }

So on my View I have put a label to check what I thought was happening. So the label shows right the total but when I use the ListView binging it doesn't work I don't know why it is null there.

enter image description here

As you can see the label works fine but where it sais "Monto Total" it shows nothing. Please help I don't know what is going on. Thanks.

EDIT

I tried naming the Total property different and then when I add my Invoice entity to my list I do this

Total = Convert.ToDecimal(TotalCart),

But it isn't working eitherway. I don't know why the first time it returns the value fine but the second it doesn't.

After debbuging I have realized when getting the total property that the first time when GetListCart is being called the value is null and the second time, when the label text is set it returns the correct value. Why is this happening?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source