WPF ListView 简单的数据绑定
- <ListView Height="225" HorizontalAlignment="Left" Margin="125,58,0,0" Name="listView1" VerticalAlignment="Top" Width="288" >
- <ListView.View>
- <GridView x:Name="gridView1">
- <!--<GridViewColumn Header="UserId" DisplayMemberBinding="{Binding Path=UserId}"></GridViewColumn>-->
- <GridViewColumn Header="UserName" DisplayMemberBinding="{Binding Path=Name}"></GridViewColumn>
- <!--<GridViewColumn Header="MobilePhone" DisplayMemberBinding="{Binding Path=MobilePhone}"></GridViewColumn>
- <GridViewColumn Header="Address" DisplayMemberBinding="{Binding Path=Address}"></GridViewColumn>-->
- </GridView>
- </ListView.View>
- </ListView>
- //后台代码
- public MainWindow()
- {
- InitializeComponent();
- listView1.ItemsSource = CreateDataTable().DefaultView;
- }
- private void cbUserCategory_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
- {
- DataTable CreateDataTable()
- {
- DataTable tbl = new DataTable("Customers");
- tbl.Columns.Add("ID", typeof(int));
- tbl.Columns.Add("Name", typeof(string));
- tbl.Columns.Add("Balance", typeof(decimal));
- tbl.Rows.Add(1, "John Doe", 100m);
- tbl.Rows.Add(2, "Jane Dorkenheimer", -209m);
- tbl.Rows.Add(3, "Fred Porkroomio", 0m);
- tbl.Rows.Add(4, "Mike Spike", 550m);
- tbl.Rows.Add(5, "Doris Yakovakovich", 0m);
- tbl.Rows.Add(6, "Boris Zinkwolf", -25m);
- return tbl;
- }
- }