来自列表<对象>的组合框DisplayMemberPath

时间:2018-11-28 16:53:27

标签: c# wpf mvvm data-binding binding

我正在尝试将列表绑定到ComboBox并使用displaymemberpath显示值。

这是我的XAML:

<ComboBox x:Name="ComboBoxCommissieGroep" 
           ItemsSource="{Binding Commissies}" 
           DisplayMemberPath="CommissieGroep">

ViewModel:

我的ViewModel检索“漫画”列表

private async Task LoadData()
{
    commissies = new ObservableCollection<object>(await LoginBLL.GetCommissiesList());
}

型号:

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();
        }
    }
}

我的代码数据库方法: 该方法创建一个已经从数据库中检索到的数据集的列表。这就是为什么它是对象类型而不是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));
}

创建列表对象的方法

private List<object> ToList(DataSet dataSet)
{
    var list = new List<object>();

    foreach (var dataRow in dataSet.Tables[0].Rows)
    {
        list.Add(dataRow);
    }
    return list;
}

我已经将DataContext设置为视图模型,并且知道绑定是有效的,因为在我的没有displaymemberpath的组合框中,它说:“ System.Data.Datarow”。那么,我必须在displaymemberpath中添加什么才能显示其值?

2 个答案:

答案 0 :(得分:1)

您可以将ToList方法更改为此:

private List<InloggenBO> ToList(DataSet dataSet)
{
    //new list of InloggenBO instead of objects
    var list = new List<InloggenBO>();

    foreach (var dataRow in dataSet.Tables[0].Rows)
    {
        //create a new instance of InloggenBO
        var item = new InloggenBO();
        //this assumes that the column is named as the member
        item.CommissieGroep = dataRow["CommissieGroep"].ToString(); 
        //fill the other members

        //add the instane of InloggenBO to the list
        list.Add(item);
    }

    return list;
}

但是,正如@Andy提到的那样,您应该考虑使用诸如Entity Framework之类的ORM。

答案 1 :(得分:1)

系统完成的绑定过程分为两个部分。第一部分是目标对象,我的意思是 object ,因为它未知,该对象由控件的DataContext提供。

如果我的控件没有设置DataContext,则它会继承XAML中指定的父控件,如果它也为null,则该过程会重复进行,直到最终获取页面的数据上下文为止。

绑定的第2步是当该对象不为null时,它随后从绑定声明本身给出的绑定 hint 反映为属性


因此,在您的情况下,它正在查找数据上下文之外的名为Commissies的属性。如果该属性不是null而是一个列表,则成功绑定到列表后,组合框将尝试在要显示的每一行中查找并显示一个名为CommissieGroep的属性。

对于组合框,为了正确显示数据,必须符合三件事。

您似乎没有正确进行数据绑定和显示的类型的正确列表(或可观察列表);解决该问题的方法。

相关问题