WPF ListView 简单的数据绑定

 

  1. <ListView Height="225" HorizontalAlignment="Left" Margin="125,58,0,0" Name="listView1" VerticalAlignment="Top" Width="288" >
  2. <ListView.View>
  3. <GridView x:Name="gridView1">
  4. <!--<GridViewColumn Header="UserId" DisplayMemberBinding="{Binding Path=UserId}"></GridViewColumn>-->
  5. <GridViewColumn Header="UserName" DisplayMemberBinding="{Binding Path=Name}"></GridViewColumn>
  6. <!--<GridViewColumn Header="MobilePhone" DisplayMemberBinding="{Binding Path=MobilePhone}"></GridViewColumn>
  7. <GridViewColumn Header="Address" DisplayMemberBinding="{Binding Path=Address}"></GridViewColumn>-->
  8. </GridView>
  9. </ListView.View>
  10. </ListView>
  11. //后台代码
  12. public MainWindow()
  13. {
  14.   InitializeComponent();
  15.   listView1.ItemsSource = CreateDataTable().DefaultView;
  16. }
  17. private void cbUserCategory_SelectionChanged_1(object senderSelectionChangedEventArgs e)
  18.         {
  19.            
  20. DataTable CreateDataTable()
  21. {
  22. DataTable tbl = new DataTable("Customers");
  23. tbl.Columns.Add("ID", typeof(int));
  24. tbl.Columns.Add("Name", typeof(string));
  25. tbl.Columns.Add("Balance", typeof(decimal));
  26. tbl.Rows.Add(1, "John Doe", 100m);
  27. tbl.Rows.Add(2, "Jane Dorkenheimer", -209m);
  28. tbl.Rows.Add(3, "Fred Porkroomio", 0m);
  29. tbl.Rows.Add(4, "Mike Spike", 550m);
  30. tbl.Rows.Add(5, "Doris Yakovakovich", 0m);
  31. tbl.Rows.Add(6, "Boris Zinkwolf", -25m);
  32. return tbl;
  33. }
  34.         }
posted @ 2012-02-03 14:38  @张凯@  阅读(5084)  评论(0编辑  收藏  举报