Pochmurnik
Pochmurnik

Reputation: 828

DisplayMemberPath and SelectedValuePath issue while using LINQ to generate ComboBox.ItemsSource

XAML code:

<ComboBox Name="comboBoxAccountName" DisplayMemberPath="AccountName" SelectedValuePath="AccountID" />

ItemComboBoxAccount class:

public class ItemComboBoxAccount
{
    private int accountID;
    private String accountName;

    public int AccountID
    {
        get { return accountID; }
        set { accountID = value; }
    }

    public String AccountName
    {
        get { return accountName; }
        set { accountName = value; }
    }
}

LINQ query:

comboBoxAccountName.ItemsSource = (from accounts in dataContext.Accounts
                                  select new { accountName = accounts.accountName, accountID = accounts.accountID }
                                  ).AsEnumerable()
                                  .Select(x => new ItemComboBoxAccount { AccountID = x.accountID, AccountName = x.accountName })
                                  .ToList();

ComboBox shows no items, there are as many rows as accounts in Accounts, but rows remain blank. I want display AccountName.

Upvotes: 0

Views: 332

Answers (1)

Rehab
Rehab

Reputation: 26

Well,for entities I would use lambda expression to set the item source. That way I can dynamically populate the combo box from one method without having to use data binding.

I use sql and then create a instance of the db in the context.tt file.

Manipulate the following for your own use:

Going to use "BankEntities" to represent the db entity.

Try:

combobox.itemsource = BankEntities.instance.BankAccounts.where(x => x.AccountName == x.AccountName).ToList();

or

combobox.itemsource = BankEntities.instance.BankAccounts.where(x => x.AccountId == x.AccountId).ToList();

Upvotes: 1

Related Questions