0

You may read the complete structure of my solution here, but here's the quick reference:

  1. I made a class Account.cs in the Entities class library.
  2. I made a class library Core with a class AccountController.cs which gets the accounts from the Sql Server tables.
  3. I made a class AccountWindowController.cs in the Gui.Wpf.Controllers class library. It contains the List<Account> Accounts property and calls for the GetAccounts() method in the AccountController to fill that list.
  4. Finally, I made a AccountWindow.xaml in the Gui.Wpf class library. This WPF window contains a ListBox named AccountsListBox.

I want to data bind the list box from AccountWindow to the list in the AccountWindowController, but I don't know how. Here's 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);
    }
}

Thank you for all the help.

Community
  • 1
  • 1
Boris
  • 9,986
  • 34
  • 110
  • 147

2 Answers2

1

The ItemsSource needs to be an IEnumerable. The AccountsCollection resource is a class that contains the property you want to use. In order to do this, you need to bind to that 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 should also implement INotifyPropertyChanged on the AccountWindowController (and raise PropertyChanged in the Accounts setter) so that if you set the Accounts property, the ListBox will rebind to the new collection. And if the Accounts collection is modified at runtime, it should be an ObservableCollection.

Abe Heidebrecht
  • 30,090
  • 7
  • 62
  • 66
0

It looks like you're nearly there with it. Try changing

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

Into

ItemsSource="{Binding Source={StaticResource ResourceKey=AccountsCollection}, Path=Accounts}" />
Matthew Steeples
  • 7,858
  • 4
  • 34
  • 49
  • Thanks for the answer Matthew. Changing the code resulted in the compile error with the error message: Error Unknown property 'Path' for type 'System.Windows.StaticResourceExtension' encountered while parsing a Markup Extension. Should I change something else as well? – Boris Nov 23 '10 at 19:04
  • Apologies, I'd missed a bit out. Please try again. – Matthew Steeples Nov 23 '10 at 20:34