WPF - 多列ListView添加数据的多种方式

多列ListView:

  

        <ListView x:Name="listView">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="序号" Width="50"/>
                    <GridViewColumn Header="姓名" Width="90"/>
                    <GridViewColumn Header="成绩" Width="90"/>
                </GridView>
            </ListView.View>
        </ListView>

多列ListView填充数据:

一、使用DataTable填充

xml代码:

        <ListView x:Name="listView" ItemsSource="{Binding}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="序号" Width="50" DisplayMemberBinding="{Binding Num}"/>
                    <GridViewColumn Header="姓名" Width="90" DisplayMemberBinding="{Binding Name}"/>
                    <GridViewColumn Header="成绩" Width="90" DisplayMemberBinding="{Binding Score}"/>
                </GridView>
            </ListView.View>
        </ListView>

cs代码:

using System.Data;

DataTable dt = new DataTable(); dt.Columns.Add("Num"); dt.Columns.Add("Name"); dt.Columns.Add("Score"); dt.Rows.Add(0,"章鱼",126); dt.Rows.Add(1,"胖虎",114); dt.Rows.Add(2,"暴君",128); listView.DataContext = dt;

效果:

二、使用匿名对象填充

xml代码:

        <ListView x:Name="listView">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="序号" Width="50" DisplayMemberBinding="{Binding Num}"/>
                    <GridViewColumn Header="姓名" Width="90" DisplayMemberBinding="{Binding Name}"/>
                    <GridViewColumn Header="成绩" Width="90" DisplayMemberBinding="{Binding Score}"/>
                </GridView>
            </ListView.View>
        </ListView>

cs代码:

            listView.Items.Add(new { Num = "1", Name = "A", Score = "10"});
            listView.Items.Add(new { Num = "2", Name = "B", Score = "20" });
            listView.Items.Add(new { Num = "3", Name = "C", Score = "30" });

效果:

三、使用实例对象(推荐)

xml代码:

        <ListView x:Name="listView">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="序号" Width="50" DisplayMemberBinding="{Binding Num}"/>
                    <GridViewColumn Header="姓名" Width="90" DisplayMemberBinding="{Binding Name}"/>
                    <GridViewColumn Header="成绩" Width="90" DisplayMemberBinding="{Binding Score}"/>
                </GridView>
            </ListView.View>
        </ListView>

cs代码:

        public MainWindow()
        {
            InitializeComponent();

            listView.Items.Add(new StudentInfo(11, "小花", 22));
            listView.Items.Add(new StudentInfo(12, "小草", 11));
            listView.Items.Add(new StudentInfo(13, "小蚂蚁", 12));
        }
        

        class StudentInfo
        {
            public int Num { set; get; }
            public string Name { set; get; }
            public int Score { set; get; }

            public StudentInfo(int num,string name,int score)
            {
                this.Num = num;
                this.Name = name;
                this.Score = score;
            }
        }

效果:

posted @ 2018-05-27 20:47  池温希傲  阅读(16059)  评论(0编辑  收藏  举报