Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Databinding not working in label inside listView

dave_silvadave_silva Member ✭✭

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 x:Name="listViewSpentByCategory">

                <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".

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    How is Transaction?
  • dave_silvadave_silva Member ✭✭

    @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; }
    
        }
    
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    edited April 18
    You have not a quantity property in this class
  • dave_silvadave_silva Member ✭✭
    > @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.
Sign In or Register to comment.