ListView.SmallImageList 属性

定义

获取或设置 ImageList,当项在控件中显示为小图标时使用。

C#
public System.Windows.Forms.ImageList SmallImageList { get; set; }
C#
public System.Windows.Forms.ImageList? SmallImageList { get; set; }

属性值

ImageList

ImageList,包含将 View 属性设置为 SmallIcon 时要使用的图标。 默认值为 null

示例

下面的代码示例创建一个控件,其中包含为每个项指定的三ListViewItemListView对象和三ListViewItem.ListViewSubItem个对象。 该示例还创建 ColumnHeader 对象以在详细信息视图中显示子项。 在代码示例中还会创建两 ImageList 个对象,以便为 ListViewItem 对象提供图像。 这些 ImageList 对象将添加到 LargeImageList 属性中 SmallImageList 。 此示例在创建 ListView 控件时使用以下属性。

此示例要求你已将代码添加到 Form 该代码,并从窗体上的构造函数或其他方法调用在示例中创建的方法。 该示例还要求名为 MySmallImage1C MySmallImage2``MyLargeImage1MyLargeImage2映像位于驱动器 C 的根目录中。

C#
private void CreateMyListView()
{
    // Create a new ListView control.
    ListView listView1 = new ListView();
    listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));

    // Set the view to show details.
    listView1.View = View.Details;
    // Allow the user to edit item text.
    listView1.LabelEdit = true;
    // Allow the user to rearrange columns.
    listView1.AllowColumnReorder = true;
    // Display check boxes.
    listView1.CheckBoxes = true;
    // Select the item and subitems when selection is made.
    listView1.FullRowSelect = true;
    // Display grid lines.
    listView1.GridLines = true;
    // Sort the items in the list in ascending order.
    listView1.Sorting = SortOrder.Ascending;
                
    // Create three items and three sets of subitems for each item.
    ListViewItem item1 = new ListViewItem("item1",0);
    // Place a check mark next to the item.
    item1.Checked = true;
    item1.SubItems.Add("1");
    item1.SubItems.Add("2");
    item1.SubItems.Add("3");
    ListViewItem item2 = new ListViewItem("item2",1);
    item2.SubItems.Add("4");
    item2.SubItems.Add("5");
    item2.SubItems.Add("6");
    ListViewItem item3 = new ListViewItem("item3",0);
    // Place a check mark next to the item.
    item3.Checked = true;
    item3.SubItems.Add("7");
    item3.SubItems.Add("8");
    item3.SubItems.Add("9");

    // Create columns for the items and subitems.
    // Width of -2 indicates auto-size.
    listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);

    //Add the items to the ListView.
    listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3});

    // Create two ImageList objects.
    ImageList imageListSmall = new ImageList();
    ImageList imageListLarge = new ImageList();

    // Initialize the ImageList objects with bitmaps.
    imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage1.bmp"));
    imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage2.bmp"));
    imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage1.bmp"));
    imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage2.bmp"));

    //Assign the ImageList objects to the ListView.
    listView1.LargeImageList = imageListLarge;
    listView1.SmallImageList = imageListSmall;

    // Add the ListView to the control collection.
    this.Controls.Add(listView1);
}

注解

SmallImageList属性允许指定一个ImageList对象,该对象在将属性设置为除) 以外的LargeIcon任何值时, (显示带有小图标的项目时View要使用的图标。 控件 ListView 可以接受控件在显示图标时支持的任何图形格式 ImageList 。 该 ListView 控件不限于 .ico 文件。 将属性ImageList分配给SmallImageList属性后,可以将控件中的每个ListView属性ListViewItem设置为ImageIndex相应图像的ImageList索引位置。 属性指定ImageSize图标SmallImageList的大小。

因为只能为ListViewItem.ImageIndex属性指定一个索引,ImageList因此在属性中指定的LargeImageListSmallImageList对象应该具有相同的索引位置才能显示图像。 例如,如果ImageIndex某个ListViewItem属性设置为 0,则用于小图标和大图标的图像应位于指定对象和ImageListSmallImageList属性中的LargeImageList相同索引位置。

备注

使用键指定映像时,仍需将映像的小型和大型版本放置在其各自的图像列表中的同一索引位置。 在视图之间切换时,一个列表中的图像的索引位置用于查找另一个列表中的图像,而不考虑指定的键值。

若要设置在ImageList将属性设置为LargeIcon) 时View显示具有大图标的项时 (要使用的属性,请使用该LargeImageList属性。 如果要使用图像来显示项状态,请使用 StateImageList 该属性。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7

另请参阅