I am trying to bind a list to a ComboBox and use displaymemberpath to display the values.
This is my XAML:
<ComboBox x:Name="ComboBoxCommissieGroep"
ItemsSource="{Binding Commissies}"
DisplayMemberPath="CommissieGroep">
ViewModel:
My ViewModel retreives a list of "Commissies"
private async Task LoadData()
{
commissies = new ObservableCollection<object>(await LoginBLL.GetCommissiesList());
}
Model:
public class InloggenBO : ObservableObject
{
private int lidNummer;
public int LidNummer
{
get => lidNummer;
set
{
lidNummer = value;
NotifyPropertyChanged();
}
}
private string commissieGroep;
public string CommissieGroep
{
get => commissieGroep;
set
{
commissieGroep = value;
NotifyPropertyChanged();
}
}
private string wachtwoord;
public string Wachtwoord
{
get => wachtwoord;
set
{
wachtwoord = value;
NotifyPropertyChanged();
}
}
}
My code database method: The method creates a list of a dataset that has been retrieved from the database. That is why it is of type object and not InloggenBO
protected Task<List<object>> ExecuteReader(string SqlString)
{
// Read to dataset
DataSet dataset = new DataSet();
using (var conn = new SqlConnection(ConnectionString))
using (var adapter = new SqlDataAdapter(new SqlCommand(SqlString, conn)
{
// Set commandtype
CommandType = CommandType.Text
}))
{
// Open connection
conn.Open();
// Fill dataset
adapter.Fill(dataset);
// Close connection
conn.Close();
}
return Task.FromResult(ToList(dataset));
}
Method that creates the list object
private List<object> ToList(DataSet dataSet)
{
var list = new List<object>();
foreach (var dataRow in dataSet.Tables[0].Rows)
{
list.Add(dataRow);
}
return list;
}
I have set my DataContext to the viewmodel and I know that the binding works because in my combobox without the displaymemberpath it says: "System.Data.Datarow". So what do I have to put in the displaymemberpath so that I can show its value?
ToList(dataset)returns aList<DataRow>- so why should aDataRowobject know anything about yourInloggenBOclass? - Rand Random