如何绑定无数列表项

时间:2019-04-15 17:36:14

标签: c# wpf datagrid

我正在尝试使用点表示法将DataGridComboBoxColumn绑定到列表元素。我的列表中有数据,我可以在后面的代码中看到这些数据,但是我的ComboBox是空白的。

我尝试了多种版本的绑定点表示法级别。

这是我后面的代码...

            List<DatabaseProperty> dbList = new List<DatabaseProperty>();
            foreach(Database db in srv1.Databases)
            {
                DatabaseProperty newDB = new DatabaseProperty();
                newDB.DBName = db.Name.ToString();

                // Get the DB's users
                //List<DBUser> dbUserList = new List<DBUser>();

                for (int i = 0; i < srv1.Logins.Count; i++)
                {
                    // DatabaseProperty class contains a DBUser enumberable list
                    DBUser newdbUser = new DBUser();
                    //newDB.DBUserList[i].UserName = srv1.Logins[i].Name;
                    newdbUser.UserName = srv1.Logins[i].Name;

                    newDB.DBUserList.Add(newdbUser);
                }


                dbList.Add(newDB);
            }

还有WPF ...

                                     

                <DataGridComboBoxColumn Header="DB Users" ItemsSource="{Binding Path=DBUserList.DBUser.UserName }" >

                </DataGridComboBoxColumn>
            </DataGrid.Columns>
        </DataGrid>

2 个答案:

答案 0 :(得分:1)

首先创建一个公共属性以进行绑定:

public DatabaseProperty MyNewBindableDB {get;set;}

List<DatabaseProperty> dbList = new List<DatabaseProperty>();    
        foreach(Database db in srv1.Databases)
        {
            DatabaseProperty newDB = new DatabaseProperty();
            newDB.DBName = db.Name.ToString();

            // Get the DB's users
            //List<DBUser> dbUserList = new List<DBUser>();

            for (int i = 0; i < srv1.Logins.Count; i++)
            {
                // DatabaseProperty class contains a DBUser enumberable list
                DBUser newdbUser = new DBUser();
                //newDB.DBUserList[i].UserName = srv1.Logins[i].Name;
                newdbUser.UserName = srv1.Logins[i].Name;

                newDB.DBUserList.Add(newdbUser);
            }


            dbList.Add(newDB);
        }
       MyNewBindableDB = dbList.FirstOrDefault(); //gets one list from your 
        //list of <DatabaseProperty>. 

现在您的代码应类似于:

 <DataGridComboBoxColumn 
    Header="DB Users"       
    ItemsSource="{Binding Path=MyNewBindableDB.DBUserList}"
    SelectedValuePath="UserName" 
    DisplayMemberPath="UserName">
  </DataGridComboBoxColumn>

您还可以创建类型为'DBUserList'的属性并将其绑定。

更多信息: DataGridComboBoxColumn

答案 1 :(得分:0)

放弃DataGridComboBoxColumn。

            <ComboBox ItemsSource="{Binding DBUserList}" Text="{Binding UserName}" 
                     DisplayMemberPath="UserName" IsEditable="True"></ComboBox>
         </DataTemplate>

似乎我需要TEXT和DisplayMemberPath属性来实现此目的。