MacAllen
MacAllen

Reputation: 11

Connecting a C# Array to a WPF Control 2-way

I've a struct called accounts (string account, int value), and a class called person with an arraylist of accounts (in C#).

I need the user to hit a button on the main window and have it bring up a listbox (or other control) with SOME of the person.accounts (based on some criteria, it will vary, don't want to bury my question in code when all I need is an example), have the listbox show both the account and the value (so the whole struct, not just the account number, like "10456786 $75.50"), have the user double click on one of the accounts in the listbox and have logic execute to change the value of person.account.value for that specific account (increment up or down, I have that logic, I just don't know how to connect them).

I've been wrestling with this for a week now, digging through all the books I've managed to scavenge up (new to WPF, teaching myself), and just can't make this work. I have the pieces, but not how to connect them.

It doesn't have to be Listbox, at this point I'm open to suggestions, I'm just at my wit's end on figuring out how to do this.

Thank you in advance for your patience and consideration.

Upvotes: 0

Views: 248

Answers (2)

Marcel
Marcel

Reputation: 974

I would suggest using ObservableCollections instead of ArrayLists, as they will provide notifications when they change, and your UI would update accordingly.

Then, you could do this :

Account:

public struct Account
{
  public string AccountNo { get; set; }
  public double Amount { get; set; }
}

Person:

public class Person : INotifyPropertyChanged
{
  public Person()
  {
     Accounts.Add(new Account { AccountNo = "12345", Amount = 79.50 });
     Accounts.Add(new Account { AccountNo = "23456", Amount = 19.40 });
     Accounts.Add(new Account { AccountNo = "34567", Amount = 5.60 });
     Accounts.Add(new Account { AccountNo = "45678", Amount = 109.14 });
  }

  private string _name;
  public string Name
  {
     get { return _name; }
     set
     {
        if (_name == value)
           return;
        _name = value;
        if (PropertyChanged != null)
           PropertyChanged(this, new PropertyChangedEventArgs("Name"));
     }
  }

  private List<Account> _accounts = new List<Account>();
  public List<Account> Accounts
  {
     get { return _accounts; }
     set
     {
        if (_accounts != value)
        {
           _accounts = value;
           if (PropertyChanged != null)
              PropertyChanged(this, new PropertyChangedEventArgs("Accounts"));
        }
     }
  }

  public event PropertyChangedEventHandler PropertyChanged;

}

With some sort of ListBox ItemTemplating, like

<ListBox ItemsSource="{Binding People}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{Binding Name}" />
                    <ListBox ItemsSource="{Binding Accounts}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding AccountNo}" />
                                    <TextBlock Text="{Binding Amount}" />
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                    <TextBlock Text="{Binding Amount}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Upvotes: 1

Schu
Schu

Reputation: 1164

Wpf runs best on bindings. For bindings to work, you need the underlying properties to be notifiable. So try adding a wrapper (ModelView?) around your struct properties and provide notifiable properties.

Upvotes: 0

Related Questions