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?
You can't nest a listview within a Listview and your 2nd binding would be {Binding BankAccounts.Transactions}
Answers
You can't nest a listview within a Listview and your 2nd binding would be {Binding BankAccounts.Transactions}
Also BankAccount probably needs to be public.
where do you set your bindingcontext?
And while we are at it, the backing properties should probably be observable collections and the ViewModels and stuff do INotifyPropertyCHanged.
Amen
https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/
Think i need to rethink the design then of the page and instead separate the actual account and the account transactions.
Thanks every one for your help.