WPF data binding: how to bind data to a collection?

You can read the full structure of my solution here , but here is a short link:

  • I created the Account.cs class in the Entities class library.
  • I created a Core class library with the AccountController.cs class, which retrieves accounts from Sql server tables.
  • I created the AccountWindowController.cs class in the AccountWindowController.cs class library. It contains the List<Account> Accounts property and calls the GetAccounts() method in the AccountController to populate this list.
  • Finally, I created AccountWindow.xaml in the Gui.Wpf class Gui.Wpf . This WPF window contains a ListBox named AccountsListBox .

I want the data to bind the list box from AccountWindow to the list in AccountWindowController , but I don't know how to do this. Here is the relevant code:

AccountWindow.xaml

 <Window x:Class="Gui.Wpf.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controller="clr-namespace:Gui.Wpf.Controllers" Title="Accounts" Width="350" MinWidth="307" MaxWidth="400" Height="500" > <Window.Resources> <controller:AccountWindowController x:Key="AccountsCollection" /> </Window.Resources> <Grid> <ListBox Name="AccountsListBox" Margin="12,38,12,41" ItemsSource="{StaticResource ResourceKey=AccountsCollection}" /> </Grid> </Window> 

AccountWindow.xaml.cs

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); new Gui.Wpf.Controllers.AccountWindowController(); } } 

AccountWindowController.cs

 public class AccountWindowController { //This event is handled in the AccountController.cs //that sets the Accounts property defined below. public event EventHandler GetAccounts; private List<Account> accounts; public List<Account> Accounts { get { GetAccounts(this, new EventArgs()); return accounts; } set { this.accounts = value; } } //Constructor public AccountWindowController() { new AccountController(this); } } 

Thanks for the help.

0
data-binding wpf binding
source share
2 answers

ItemsSource must be IEnumerable . An AccountsCollection resource is a class that contains the property that you want to use. To do this, you need to bind this property and use the resource as the source of the binding:

 <ListBox Name="AccountsListBox" Margin="12,38,12,41" ItemsSource="{Binding Accounts, Source={StaticResource ResourceKey=AccountsCollection}}" /> 

You must also implement INotifyPropertyChanged in the AccountWindowController (and raise the PropertyChanged in the account installer) so that when you set the Accounts property, the ListBox will double-check the new collection. And if the account collection is changed at run time, it should be an ObservableCollection .

+1
source share

Sounds like you're almost there with him. Try to change

 ItemsSource="{StaticResource ResourceKey=AccountsCollection}" /> 

IN

 ItemsSource="{Binding Source={StaticResource ResourceKey=AccountsCollection}, Path=Accounts}" /> 
0
source share

All Articles