ObjectListView显示图标

本文关键字:图标 显示图 显示 ObjectListView | 更新日期: 2023-09-27 18:05:29

尝试在ObjectListview中放置图标,这里是我应该放置图标的代码:

objectListView1.SmallImageList = imageList1;
        deleteColumn.IsEditable = true;
        deleteColumn.ImageGetter = delegate
        {
            return 0;
        };
        deleteColumn.AspectGetter = delegate
        {
            return "Delete";
        };

imageList1已经有一个图像,这个代码应该在"删除"旁边放一个图标,但它根本没有出现,通过烹饪书和谷歌看,我仍然不知道。有人能帮我吗?

如果需要,这是完整的表单代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
        objectListView1.AllowDrop = true;
        objectListView1.DragEnter += new DragEventHandler(objectListView1_DragEnter);
        objectListView1.DragDrop += new DragEventHandler(objectListView1_DragDrop);
        objectListView1.CellEditActivation = BrightIdeasSoftware.ObjectListView.CellEditActivateMode.SingleClick;
        objectListView1.CellEditStarting += deleteItems;
        objectListView1.SmallImageList = imageList1;
        deleteColumn.IsEditable = true;
        deleteColumn.ImageGetter = delegate
        {
            return 0;
        };
        deleteColumn.AspectGetter = delegate
        {
            return "Delete";
        };
    }
    private void objectListView1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effect = DragDropEffects.Copy;
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }
    private void objectListView1_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
        {
            string[] droppedFiles = (string[]) e.Data.GetData(DataFormats.FileDrop);
            foreach (string path in droppedFiles)
            {
                if (File.Exists(path))
                {
                    FileObject fo = new FileObject(path, "added later"); 
                    objectListView1.AddObject(fo);
                }
            }
        }
    }
    private void deleteItems(object sender, BrightIdeasSoftware.CellEditEventArgs e)
    {
        if(e.Column == deleteColumn)
        {
            e.Cancel = true;
            objectListView1.RemoveObject(e.RowObject);
        }
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    }
}

}

ObjectListView显示图标

为了使图像出现在列中的文本旁边,您必须:

  1. 连接ObjectListViewImageList(使用SmallImageList属性);
  2. 为必须显示图像的列安装ImageGetter代表;
  3. 确保ImageList中有实际的图像。

完成后,图像将出现(我刚刚测试了这个)。

但是有一个问题。从你的问题来看,我怀疑"删除"列可能不是ObjectListView中的第一列。以上步骤只允许您在第一列中显示图像。对于后面的列,必须将ShowImagesOnSubItems属性设置为True。会是这样吗?