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

大城市里の小女人 提交于 2019-12-24 22:36:13

问题


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.


回答1:


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();


来源:https://stackoverflow.com/questions/28926473/displaymemberpath-and-selectedvaluepath-issue-while-using-linq-to-generate-combo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!