Forum Xamarin.Forms

Can't databind a ListView in XAML

ThreezoolThreezool USMember ✭✭

Hi!

I'm having a issue when I'm trying to bind to a collection in the ViewModel with ListView ItemSource.

This is the model I'm using to bind to the XAML:

class BankAccountInfo
{
    public string AccountName { get; set; }
    public long AccountNumber { get; set; }
    public List<BankTransaction> Transactions { get; set; }

    Other stuff here....
}

This is my ViewModel:

class BankViewModel
{
    public List<BankAccountInfo> BankAccounts => bankAccount.Accounts;

    private readonly BankAccount bankAccount;

    public BankViewModel()
    {
        this.bankAccount = new BankService(App.CurrentUser.BankUserName, App.CurrentUser.BankPassword).GetAccountInfoFrom(App.CurrentUser.Bank);
    }
}

This is then being binded to the binding context in the code behind and then when I'm trying to fetch that binding all the data is empty, this is my XAML file:

<ListView ItemsSource="{Binding BankAccounts}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid Margin="10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="2"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*"/>
                        <ColumnDefinition Width="1*"/>
                        <ColumnDefinition Width="1*"/>
                    </Grid.ColumnDefinitions>
                    <BoxView BackgroundColor="Gray" Opacity="0.5" Grid.Row="1" Grid.Column="0"/>
                    <Label Text="{Binding AccountName}" Grid.Row="1" Grid.Column="0"/>
                    <Label Text="{Binding AccountNumber}" Grid.Row="1" Grid.Column="2"/>
                    <BoxView BackgroundColor="Gray" Opacity="0.5" Grid.Row="1" Grid.Column="2"/>

                    <StackLayout Orientation="Horizontal" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3">
                        <Label Text="Datum"/>
                        <Label Text="Summa"/>
                    </StackLayout>
                    <ListView ItemsSource="{Binding Transactions}" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="3">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <StackLayout Orientation="Horizontal">
                                        <Label Text="{Binding TransactionDate, StringFormat='{0:YYYY-MM-DD}'}"/>
                                        <Label Text="{Binding AmountChanged}"/>
                                    </StackLayout>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

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

Any thoughts on what i am doing wrong here?

Best Answer

Answers

Sign In or Register to comment.