wpf 直接用 LINQ 查询的结果填充 ComboBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31880767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fill ComboBox with Results of LINQ Query, Directly
提问by Dan.
I am new to c#/.net/WPF.
我是 c#/.net/WPF 的新手。
I am trying to fill a combobox with values taken from a database.
我正在尝试使用从数据库中获取的值填充组合框。
The LINQ query gets a list of all companies in the database and the code attempts to fill a ComboBox control with this list.
LINQ 查询获取数据库中所有公司的列表,代码尝试使用此列表填充 ComboBox 控件。
The C# code below successfully gets the results (I previously outputted it with a MessageBox.Show()).
下面的 C# 代码成功获取了结果(我之前用 MessageBox.Show() 输出了它)。
My Next step was to remove that bit and, instead, put in the code which would fill out this ComboBox:
我的下一步是删除该位,而是输入将填写此 ComboBox 的代码:
<ComboBox Name="companyComboBox"/>
The c#:
C#:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace LeadSystem
{
/// <summary>
/// Interaction logic for NewLead.xaml
/// </summary>
public partial class NewLead : Window
{
public NewLead()
{
// Use a connection string.
DataContext db = new DataContext("Data Source=HP\SQLEXPRESS;Initial Catalog=LeadSystem;Integrated Security=True");
// Get a typed table to run queries.
Table<Company> Companies = db.GetTable<Company>();
// Attach the log to show generated SQL.
db.Log = Console.Out;
// Query for all companies.
var companyQuery =
from c in Companies
select new { Name = c.CompanyName, ID = c.CompanyID };
companyComboBox.ItemsSource = companyQuery.ToList();
companyComboBox.DisplayMemberPath = "Name";
companyComboBox.SelectedValuePath = "ID";
InitializeComponent();
}
}
}
The problem I keep getting is:
我不断遇到的问题是:
Object reference not set to an instance of an object.
^ it's talking about companyQuery, where I try to use it to fill the comboBox.
^ 这是关于 companyQuery 的,我尝试用它来填充组合框。
I thought this must be because of deferred execution, and so I had a look around the web to find a solution. I've seen several people say to add ToList() at the end of that line of code, but nothing changed.
我认为这一定是因为延迟执行,所以我在网上找了一个解决方案。我看到有几个人说要在该行代码的末尾添加 ToList(),但没有任何改变。
So, does someone here know what I'm doing wrong??
那么,这里有人知道我做错了什么吗?
I have looked around the web (including Stackoverflow) and nothing has helped me fix mine.
我环顾了网络(包括 Stackoverflow),但没有任何帮助我修复我的。
Also, if it's not too cheeky to ask two questions in one go... How do I set the selected value and displayed values in my ComboBox? Is it correct, the way I already have it?
另外,如果一次性问两个问题不是太厚颜无耻的话...如何在我的 ComboBox 中设置选定的值和显示的值?是否正确,我已经拥有它的方式?
Thanks
谢谢
回答by Chino
Try to fill the companyComboBox after the initializeComponent
尝试在 initializeComponent 之后填充 companyComboBox
InitializeComponent();
companyComboBox.ItemsSource = companyQuery.ToList();
companyComboBox.DisplayMemberPath = "Name";
companyComboBox.SelectedValuePath = "ID";
回答by Reece Kenney
Put InitializeComponent();before you set the itemSource
把InitializeComponent();你设置的前itemSource

