locked
Databinding not working in label inside listView RRS feed

  • Question

  • User393905 posted

    Hello there, I'm using a ListView with interaction with database but currently I'm having a problem while binding some data.

    Here's my query on database :

    public async Task<List<Transaction>> GetSpentByCategory() { return await _database.QueryAsync<Transaction>("SELECT Category,Amount , count(*) as Quantity FROM 'Transaction' WHERE Type = False GROUP BY Category ORDER BY Amount;"); }

    And my xaml : ```

                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout
                        Orientation="Vertical">
                                <StackLayout Orientation="Horizontal">
                                    <StackLayout Orientation="Vertical">
    
                                        <Label Text="{Binding Category}"
                                HorizontalOptions="Start"
                                TextColor="#000000" />
    
                                        <Label Text="{Binding Quantity}"
                                HorizontalOptions="Start"
                                TextColor="#000000" />
                                    </StackLayout>
    
    
                                    <Label Text="{Binding Amount}"
                                HorizontalOptions="EndAndExpand"
                                TextColor="#000000" />
    
                                    <Label Text="CHF"
                                HorizontalOptions="End"
                                TextColor="#000000" />
    
                                </StackLayout>
                            </StackLayout>
                        </ViewCell>
    
                    </DataTemplate>
    
                </ListView.ItemTemplate>
    
            </ListView>
    

    ```

    So I can correctly bind Amount and Category, but not Quantity. Do you have any idea why ? Quantity is not a column on my database, that's why my query has "as Quantity".

    Saturday, April 18, 2020 3:18 PM

Answers

  • User369979 posted

    You still need to define a Quantity property in your model so that it could be bound by the label.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, April 21, 2020 9:37 AM

All replies

  • User2148 posted

    How is Transaction?

    Saturday, April 18, 2020 7:12 PM
  • User393905 posted

    @AlessandroCaliaro said: How is Transaction?

    Transaction looks like this in my database.

    ``` public class Transaction { [PrimaryKey, AutoIncrement] public int ID { get; set; } public bool Type { get; set; } public string Name { get; set; } public string Category { get; set; } public float Amount { get; set; } public string Date { get; set; }

    }
    

    ```

    Saturday, April 18, 2020 7:23 PM
  • User2148 posted

    You have not a quantity property in this class

    Saturday, April 18, 2020 9:18 PM
  • User393905 posted

    @AlessandroCaliaro said: You have not a quantity property in this class Yes but isnt it right using « as Quantity »? If not , how can I bind that value? For the record, I’d like to get number of transactions by category.

    Saturday, April 18, 2020 9:42 PM
  • User369979 posted

    You still need to define a Quantity property in your model so that it could be bound by the label.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, April 21, 2020 9:37 AM