使用LINQ生成ComboBox.ItemsSource时出现DisplayMemberPath和SelectedValuePath问题 [英] DisplayMemberPath and SelectedValuePath issue while using LINQ to generate ComboBox.ItemsSource

查看:102
本文介绍了使用LINQ生成ComboBox.ItemsSource时出现DisplayMemberPath和SelectedValuePath问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XAML代码:

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

ItemComboBoxAccount类:

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查询:

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

组合框不显示任何项目,帐户"中的行与帐户一样多,但行仍为空白.我要显示AccountName.

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

推荐答案

好,对于实体,我将使用lambda表达式设置项目来源.这样,我可以从一种方法动态填充组合框,而不必使用数据绑定.

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.

我使用sql,然后在context.tt文件中创建数据库的实例.

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

操作以下内容以供自己使用:

Manipulate the following for your own use:

将使用"BankEntities"来表示数据库实体.

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

尝试:

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

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

这篇关于使用LINQ生成ComboBox.ItemsSource时出现DisplayMemberPath和SelectedValuePath问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆