黑马程序员技术交流社区
标题:
有没有人给详解下list用法?自学视频上提到的很少
[打印本页]
作者:
SUN_Q
时间:
2013-7-15 21:08
标题:
有没有人给详解下list用法?自学视频上提到的很少
1、数组中list的用法。
2、wpf中listBox用法。
作者:
leayon
时间:
2013-7-15 23:29
List<T>是一个泛型集合,相当于是数组的升级版,具体用法是List<T> myList = new List<T>();其中T表示存入myList集合中的元素的类型,不是这个类型的元素无法存入该集合中,我们只需要用myList.Add()方法就可以该集合中存入T类型的元素。
ListBox是一个列表框控件,用于显示一个完整的列表项,用户可以从中选择一个或多个选项。
作者:
清心玉质
时间:
2013-7-16 11:46
List的用法:
1、List中可以添加任何对象;
2、List是一个接口,不能实例化,需要实例化一个ArrayList 如List b1 = new ArrayList();
3、使用myList.add(b1);
4、取值的时候b1.get(索引);取出来的值都是Objectl类型,使用时需要类型转换。b1 p=(b1)list.get();
一、ListBox系列索引
1、WPF ListBox基础(包括ListBox多列展示,ListBox实现分页效果,ListBox绑定XML数据源)
2、ListBox 单击变大动画效果(使用模板、样式、绑定数据源等)
二 ListBox基础:包括ListBox多列展示,ListBox实现分页效果,ListBox绑定XML数据源。
在使用LsitBox这个控件的时候,如果添加数据绑定,只需要将要显示的结构体绑定到 ItemsSource 就可以将结构体成员显示出来。
作者:
彭家贰小姐
时间:
2013-7-16 15:29
本帖最后由 彭家贰小姐 于 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.结果查看
RTX截图未命名.png
(549.03 KB, 下载次数: 0)
下载附件
2013-7-16 15:28 上传
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2