I have ListBox
and want to put values in this listbox from a DataTable
:
listBoxVisibleFields.DataContext = SelectedFields;
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...
Does someone know how to fill simply my ListBox
from DataTable
?
The property you are looking for is
ItemsSource
instead ofDataContext
. The property most closely resembling ValueMember is calledSelectedValuePath
(see this example). The analogon for DisplayMember is calledDisplayMemberPath
.EDIT: So, your code should look like this:
Alternatively, the two path values can be set in XAML
which is a bit more elegant.
Here is my code, hope is usefull