以编程方式将列添加到WPF中的listview?

时间:2009-05-15 11:42:21

标签: c# wpf listview

如何以编程方式将列添加到列表视图?

3 个答案:

答案 0 :(得分:28)

将listView.View设置为:

GridView myGridView = new GridView();
myGridView.AllowsColumnReorder = true; 
myGridView.ColumnHeaderToolTip = "Employee Information";

GridViewColumn gvc1 = new GridViewColumn();
gvc1.DisplayMemberBinding = new Binding("FirstName");
gvc1.Header = "FirstName";
gvc1.Width = 100;
myGridView.Columns.Add(gvc1);
GridViewColumn gvc2 = new GridViewColumn();
gvc2.DisplayMemberBinding = new Binding("LastName");
gvc2.Header = "Last Name";
gvc2.Width = 100;
myGridView.Columns.Add(gvc2);
GridViewColumn gvc3 = new GridViewColumn();
gvc3.DisplayMemberBinding = new Binding("EmployeeNumber");
gvc3.Header = "Employee No.";
gvc3.Width = 100;
myGridView.Columns.Add(gvc3);

listView.View = myGridView;

答案 1 :(得分:3)

你可以试试这个;

GridView _gridView = new GridView();
_gridView.Columns.Add(
    new GridViewColumn 
    {
        DisplayMemberBinding = new Binding("columnTitle"), 
        Header = "columnHeader", 
        Width = double.NaN 
    });

答案 2 :(得分:0)

试试这个:

            for(int i=0; i < dt.Columns.Count; i++)
                {
                    GridViewColumn gvc = new GridViewColumn();
                    gvc.Header = "Column"+i;
                    gvc.DisplayMemberBinding = new Binding("column"+i);
                    lvgvc.Columns.Add(gvc);
                }