标签:
后台代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Data ; namespace Wpf数据绑定 { /// <summary> /// Window3.xaml 的交互逻辑 /// </summary> public partial class Window3 : Window { public Window3() { InitializeComponent(); DataTable dt = create(); for (int i = 0; i < 10; i++) { DataRow dr = dt.NewRow(); dr[0] = i; dr[1] = "姓名" + i.ToString(); dr[2] = "性别" + i.ToString(); dr[3] = "年龄" + i.ToString(); dr[4] = "班级" + i.ToString(); dt.Rows.Add(dr); } this.list .DataContext = dt; this.list.SetBinding(ListView.ItemsSourceProperty,new Binding()); } public DataTable create() { DataTable dt = new DataTable(); DataColumn[] dc = new DataColumn[] { new DataColumn("Sno"), new DataColumn("Sname"), new DataColumn("Ssex"), new DataColumn("Sbirthdy"), new DataColumn("Class") }; dt.Columns.AddRange(dc); return dt; } } }
前台代码
<Window x:Class="Wpf数据绑定.Window3" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window3" Height="428.358" Width="482.09"> <Grid> <ListView Name="list" HorizontalAlignment="Left" Height="378" Margin="43,10,0,0" VerticalAlignment="Top" Width="390"> <ListView.View> <GridView> <GridViewColumn Header=" Sno" Width=" 60" DisplayMemberBinding="{Binding Sno}"/> <GridViewColumn Header=" Name" Width=" 60" DisplayMemberBinding="{Binding Name}"/> <GridViewColumn Header=" Sex" Width=" 60" DisplayMemberBinding="{Binding Ssex}"/> <GridViewColumn Header=" Sbirthdy" Width=" 60" DisplayMemberBinding="{Binding Sbirthdy}"/> <GridViewColumn Header=" Class" Width=" 60" DisplayMemberBinding="{Binding Class}"/> </GridView> </ListView.View> </ListView> </Grid> </Window>
标签:
原文地址:http://www.cnblogs.com/275147378abc/p/4607069.html