如何将列表框绑定到 WPF 应用程序中的数据表/
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12363369/
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
How can I bind a listbox to a data table in WPF applications/
提问by Abdullah
I am trying to put the content of the one column table into the listbox. I use this code to do that.
我正在尝试将一列表的内容放入listbox. 我使用这段代码来做到这一点。
listBox1.DataContext = ds.Tables[0];
I also tried to use this code
我也尝试使用此代码
listBox1.ItemsSource = ds.Tables[0];
and both didn't work.
两者都不起作用。
Any idea how can I do that?
知道我该怎么做吗?
namespace itinventory
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
SqlConnection con ;
SqlDataAdapter da ;
DataSet ds;
public MainWindow()
{
InitializeComponent();
con = new SqlConnection("Server=myserver\sqlexpress;" + "Database=ITINVENTORY;User ID=sa;" + "Password=mypassword");
con.Open();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("select DISTINCT orgcode from Employees", con);
DataSet ds = new DataSet();
da.Fill(ds,"orgcode");
listBox1.DataContext = ds.Tables[0];
}
}
}
回答by JSJ
here is optimized codes and some suggessions for you.
这里是优化的代码和一些给你的建议。
string constring = @"Data Source=.\SQLEXPRESS;AttachDbFilename='D:\TEMPX\Projects X\ODataClient\ODataClient\Data\northwnd.mdf';Integrated Security=True;User Instance=True";
void button1_Click(object sender, RoutedEventArgs e)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from Employees", con);
da.Fill(dt);
con.Close();
}
listBox1.ItemsSource = dt.AsDataView();
}
i will not tell you about the how to write the queries. but for your question here are some points.
我不会告诉你如何编写查询。但对于你的问题,这里有一些要点。
If you are using binding then dataContext is ok. The DataContext means you are passing a object which can be further bindable to its child properties. and you neec to bind the ItemsSource Property of ListBox. as
如果您使用绑定,则 dataContext 没问题。DataContext 意味着您正在传递一个可以进一步绑定到其子属性的对象。并且您需要绑定 ListBox 的 ItemsSource 属性。作为
ItemsSource={Binding}.
项目来源={绑定}。
<ListBox x:Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FirstName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
But if you are just passing the Data to List what you need to do is asssing data into ItemsSource property and that will only show the object type Full Name insteed of showing data. Use above template to show data.
但是,如果您只是将数据传递给列表,您需要做的是将数据分配到 ItemsSource 属性中,这将只显示对象类型全名而不是显示数据。使用上面的模板来显示数据。
another thing is that when you assing DataTable object to a itemsssource/ DataContext the source must be Enumrable so use this as DataView. its insherited from Ilist , IEnumrable. etc.
另一件事是,当您将 DataTable 对象分配给 itemsssource/DataContext 时,源必须是可枚举的,因此将其用作 DataView。它继承自 Ilist 和 IEnumrable。等等。

