ObjectListView显示图标

时间:2013-03-22 08:34:02

标签: c# icons objectlistview

尝试将图标放在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)
    {

    }

}

}

1 个答案:

答案 0 :(得分:6)

要使图像显示在列中文本旁边,您必须:

  1. ObjectListView连接到ImageList(使用SmallImageList属性);
  2. 为必须显示图片的列安装ImageGetter委托;
  3. 确保ImageList
  4. 中确实存在图像

    完成此操作后,将出现图像(我刚刚对其进行了测试)。

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