C# adding multiple images to same column in objectlistview

懵懂的女人 提交于 2020-04-18 06:50:34

问题


I try to add image to my first column(it might be change to another column later, it's in first column for now), so far I have do

if (item.Index == 0)
{
    item.ImageGetter = delegate (object RowObj)
    {
        return ((RowObjectModel)RowObj).ImageToShow.ToString();
    };
}

this part at start, I use a custom headerstyle and apply it on constructor while I do that I also do ImageGetter part. I also set my SmallImageList like this

ImageList IList = new ImageList();
IList.Images.Add("MyIcon", Properties.Resources.MyIcon);
mainForm.objListview.SmallImageList = IList;

I have 2 problems with this code, first I can't set my image. It's not showing on my listview. What I do to achieve that is this :

(objListview.GetItem(z).RowObject as RowObjectModel).ImageToShow = ThumbnailImages.MyIcon;

my enum is like this :

public enum ThumbnailImages
{
    NULL = 0,
    MyIcon = 1,
    MyIcon2 = 2,
    MyIcon3 = 3,
    MyIcon4 = 4,
    MyIcon5 = 5
}

Second problem is I have literally no clue on how I can add a second image in the same column of same row. I'm not even sure if this is possible.. But I have to do it somehow so I'm open to any ideas.

EDIT : Okay I found the solution to my first problem. I was not using the UpdateObject/UpdateObjects method. I marked all my items with proper images they should show and use this method and everything worked. Now all I need is to find a way to show 2 images at the same time in 1 cell.

EDIT 2 : About my second problem I found this class --> ImagesRenderer

http://objectlistview.sourceforge.net/cs/ownerDraw.html#imagesrenderer

But I could not found any working solution so far and I don't have any clue on how this is working?


回答1:


Now all I need is to find a way to show 2 images at the same time in 1 cell.

You can work around the requirement to show 2 images by showing 1 image that contains both. The example below does that. One note is that you should probably build a cache up front of all the combinations so you can return them at high speed rather than building them in the ImageGetter delegate

olv.ShowGroups = false;

//make 2 images
var img1 = new Bitmap(10, 10);
var g = Graphics.FromImage(img1);
g.FillRectangle(new SolidBrush(Color.Pink),2,2,8,8 );

var img2 = new Bitmap(10, 10);
var g2 = Graphics.FromImage(img2);
g2.FillRectangle(new SolidBrush(Color.Blue),2,2,8,8 );

var col1 = new OLVColumn();
col1.AspectName = "ToString";

col1.ImageGetter += delegate(object rowObject)
{
    if(rowObject == "1")
        return img1;

    if(rowObject == "2")
        return img2;

    if (rowObject == "3")
    {
        var comboImg = new Bitmap(img1.Width + img2.Width, img1.Height + img2.Height);
        using (var g3 = Graphics.FromImage(comboImg))
        {
            g3.DrawImage(img1,new Point(0,0));
            g3.DrawImage(img2,new Point(img1.Width,0));
        }

        return comboImg;
    }


    return null;
};

olv.Columns.Add(col1);

olv.AddObject("1");
olv.AddObject("2");
olv.AddObject("3");

}



回答2:


The solution I found thanks to @Thomas N was this --->

In my objectlistview initializer I did this :

item.Renderer = new ImageRenderer();
item.AspectGetter = delegate (object RowObj)
{
    return ((RowObjectModel)RowObj).ImagesToShow;
};

item represent here column 0. I'm iterating all my columns to apply HeaderFormatStyle. I figure I could do this here too.

and before I do this initializer I also set my images

ImageList IList = new ImageList();
IList.Images.Add("NULL", Properties.Resources.Null);
IList.Images.Add("MyIcon", Properties.Resources.MyIcon);
IList.Images.Add("MyIcon2", Properties.Resources.MyIcon2);
IList.Images.Add("MyIcon3", Properties.Resources.MyIcon3);
IList.Images.Add("MyIcon4", Properties.Resources.MyIcon4);
IList.Images.Add("MyIcon5", Properties.Resources.MyIcon5);
objListview.SmallImageList = IList;

and all I have to do is calling my method I wrote inside my model class

public void AddThumbnailImage(ThumbnailImages tImage)
        {
            if (tImage == ThumbnailImages.NULL)
            {
                ImagesToShow.Clear();
            }
            else
            {
                if (!ImagesToShow.Contains((int)tImage))
                    ImagesToShow.Add((int)tImage);
            }
        }

I know my solution is very similar to yours Thomas but I just wanted to show how I did it :)



来源:https://stackoverflow.com/questions/58093834/c-sharp-adding-multiple-images-to-same-column-in-objectlistview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!