本帖最后由 彭家贰小姐 于 2013-7-16 15:31 编辑
一.数组是数组 list是list
集合类 List
1.定义 List<int> list = new List<int>;
2.添加值 list.Add(1);
list.Add(2);
3.遍历
foreach(int i in list){
}
4.长度 list.Count
5.删除 (1) 某个数据 list.Remove(2)
(2) 所有数据 list.Clear()
没有数组高效
二.wpf中listBox
1. MainWindow.xaml- <Window x:Class="test1.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MainWindow" Height="350" Width="525" Loaded="MainWindowLoaded">
- <Grid>
- <ListBox Height="163" HorizontalAlignment="Left" Margin="12,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="217" >
- <ListBox.ItemTemplate>
- <DataTemplate>
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="{Binding Name}"/>
- <TextBlock Text="{Binding Age}"/>
- <TextBlock Text="{Binding Gender}"/>
- </StackPanel>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- </Grid>
- </Window>
复制代码 2.MainWindow.xaml.cs- using System.Collections.Generic;
- using System.Windows;
- namespace test1
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- private void MainWindowLoaded(object sender, RoutedEventArgs e)
- {
- var persons = new List<Person>
- {
- new Person {Name = "wander", Age = "27", Gender = "女"},
- new Person {Name = "doris", Age = "25", Gender = "女"},
- new Person {Name = "apple", Age = "28", Gender = "男"}
- };
- listBox1.Items.Clear();
- listBox1.ItemsSource = persons;
- }
- }
- }
复制代码 3.新增的Person.cs类- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace test1
- {
- internal class Person
- {
- public string Name { get; set; }
- public string Age { get; set; }
- public string Gender { get; set; }
- }
- }
复制代码 4.结果查看
|