代码之家  ›  专栏  ›  技术社区  ›  Brum

组合框显示列表中的MemberPath<object>

  •  1
  • Brum  · 技术社区  · 6 年前

    我正在尝试将列表绑定到组合框,并使用displaymemberpath显示值。

    这是我的XAML:

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

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

    我的代码数据库方法:

    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设置为viewmodel,并且我知道绑定可以工作,因为在没有displaymemberpath的组合框中,它会显示:“System.Data.Datarow”。那么,我必须在displaymemberpath中放入什么才能显示它的值呢?

    2 回复  |  直到 6 年前
        1
  •  1
  •   ΩmegaMan    6 年前

    对象 对于它未知的对象,该对象由 DataContext

    数据上下文 设置时,它将继承XAML中指定的父控件,如果该控件也为null,则该过程将重复自身,直到最终获取页面的datacontext。

    绑定的第2步是当该对象不为null时,它将反射到 从装订 暗示


    Commissies 关闭数据上下文。如果该属性不为null且为列表,则成功绑定到列表后的组合框将尝试查找并显示名为的属性 CommissieGroep

    在组合框的情况下,为了正确显示数据,有三件事情必须保持一致。

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

        2
  •  1
  •   Rand Random    6 年前

    你可以改变你的想法 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;
    }