WPF ListBox - 如何从dataTable中放置值? [英] WPF ListBox - how to put values from dataTable?

查看:295
本文介绍了WPF ListBox - 如何从dataTable中放置值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 ListBox ,并希望从 DataTable 中将值放在此列表框中:

I have ListBox and want to put values in this listbox from a DataTable:

listBoxVisibleFields.DataContext = SelectedFields;

其中 SelectedFields 是一个 DataTable 填充数据。但是这个代码不行。我的 ListBox 是空的。我记得,在WinForms中,像$ code> ValueMember 和 DisplayMember 的列表框一样,但是在WPF中我没有找到有人这样...

Where SelectedFields is a DataTable filled with data. But this code does not work. My ListBox is empty. As I remember, in WinForms was sucha a thing for list box like ValueMember and DisplayMember, but in WPF I dont find something like that...

有人知道如何填写我的 ListBox DataTable

Does someone know how to fill simply my ListBox from DataTable?

推荐答案

您正在寻找的属性是 ItemsSource 而不是 DataContext 。与ValueMember最相似的属性称为 SelectedValuePath (请参阅这个例子)。 DisplayMember的类比例称为 DisplayMemberPath

The property you are looking for is ItemsSource instead of DataContext. The property most closely resembling ValueMember is called SelectedValuePath (see this example). The analogon for DisplayMember is called DisplayMemberPath.

编辑:你的代码应该如下所示:

So, your code should look like this:

DataTable SelectedFields = ...;
listBoxVisibleFields.SelectedValuePath = "myID";
listBoxVisibleFields.DisplayMemberPath = "myTextField";
listBoxVisibleFields.ItemsSource = SelectedFields.DefaultView;

或者,两个路径值可以在XAML中设置

Alternatively, the two path values can be set in XAML

<ListBox ... SelectedValuePath="myID" DisplayMemberPath="myTextField" />

这更加优雅。

这篇关于WPF ListBox - 如何从dataTable中放置值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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