1

我有一个employee包含类实例的person类:

List<Employee> emp = (AdventureWorks.Employees.Select(n => n)).ToList();

showgrid.ItemsSource = emp;
showgrid.Columns.Clear();

DataGridTextColumn data_column = new DataGridTextColumn();
data_column.Binding = new Binding("Person=>FirstName");
data_column.Header = "First Name";

showgrid.Columns.Add(data_column);

如何将对象firstname内的字段person与列绑定First Name

4

2 回答 2

0

您需要为您的员工班级中的班级人员提供公共财产。例如。

public person MyPerson {get;set;}

然后你可以用“。”绑定。在你的绑定中

data_column.Binding = new Binding("MyPerson.FirstName");
于 2013-05-13T12:51:32.110 回答
0

如果您想在代码中执行此操作,您可以执行如下所示的操作:

    private void BindDataToGrid()
    {
        //Sample Data
        List<Employee> empList =new List<Employee>();        
        empList.Add(new Employee(){FirstName = "Rob", LastName="Cruise"});
        empList.Add(new Employee() { FirstName = "Lars", LastName = "Fisher" });
        empList.Add(new Employee() { FirstName = "Jon", LastName = "Arbuckle" });
        empList.Add(new Employee() { FirstName = "Peter", LastName = "Toole" });

        DataGridTextColumn data_column = new DataGridTextColumn();
        data_column.Binding = new Binding("FirstName");
        data_column.Header = "First Name";
        showgrid.Columns.Add(data_column);

        data_column = new DataGridTextColumn();
        data_column.Binding = new Binding("LastName");
        data_column.Header = "Last Name";

        showgrid.Columns.Add(data_column);
        showgrid.ItemsSource = empList;
        showgrid.AutoGenerateColumns = false;
    }

    private class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
于 2013-05-13T12:05:56.730 回答