ObjectListView图像添加到项目/对象 [英] ObjectListView add images to items/objects

查看:676
本文介绍了ObjectListView图像添加到项目/对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 ObjectListView ,我试图将图片添加到我的项目。我把它通过所有的项目循环,然后手动编辑每个项目的图像索引工作。我想知道,如果加入项目时,这是可能的。这是我此刻的代码:



添加项

 的for(int i = 0; I< listName.Count;我++)
{
游戏NEWOBJECT =新游戏(LISTNAME [I],?);
lstvwGames.AddObject(NEWOBJECT);
}



添加图片



 的foreach(在listIcon串图标)
{
imglstGames.Images.Add(的LoadImage(图标)); //下载,然后将其转换为位图
}
的for(int i = 0; I< lstvwGames.Items.Count;我++)
{
ListViewItem的项目= lstvwGames.Items [一世];
item.ImageIndex = I;
}


解决方案

这是不完全清楚,我到底在尝试什么来实现的,但有几个方法可以映像分配到行。请注意,您可能要设置

  myOlv.OwnerDraw = TRUE; 



其也可以从设计器设置



如果你对每个模型对象,它可能是最好的一个具体形象的形象直接分配给你的对象,并使其通过属性(myObject.Image为例)访问。然后你可以使用任何行的ImageAspectName属性来指定属性名和OLV应该从中获取图像。

  myColumn .ImageAspectName =形象; 



另一种方式,它是利用一排ImageGetter做。这是更有效的,如果您的几个对象使用相同的图像,因为从任何你想要的,你可以获取图像或甚至只是返回一个索引使用指定的ImageList从OLV。

  indexColumn.ImageGetter + =委托(对象rowObject){
//这将基本上是一样的使用ImageAspectName
回报率((项目)rowObject )。图片;
};



正如所指出的,ImageGetter也可以相对于该ObjectListView的分配的ImageList返回一个指数:

  indexColumn.ImageGetter + =委托(对象rowObject){
INT imageListIndex = 0;这里

//一些逻辑
//决定基于rowObject属性或任何其他标准

返回imageListIndex要使用的图像;
};

这会重用多个对象图像的方式。


I'm using the ObjectListViewand am trying to add images to my items. I got it to work by looping through all the items and then manually editing the image index per item. I would like to know if this is possible when adding the items. This is the code I have at the moment:

Adding the items

for (int i = 0; i < listName.Count; i++)
{
    games newObject = new games(listName[i], "?");
    lstvwGames.AddObject(newObject);
}

Adding the images

foreach (string icon in listIcon)
{
    imglstGames.Images.Add(LoadImage(icon)); // Download, then convert to bitmap
}
for (int i = 0; i < lstvwGames.Items.Count; i++)
{
    ListViewItem item = lstvwGames.Items[i];
    item.ImageIndex = i;
}

解决方案

It is not entirely clear to me what exactly you try to achieve, but there are several ways to "assign" an image to a row. Note that you probably have to set

myOlv.OwnerDraw = true;

which can also be set from the designer.

If you have a specific image for each of your model objects, its probably best to assign that image directly to your object and make it accessible through a property (myObject.Image for example). Then you can use the ImageAspectName property of any row to specify that property name and the OLV should fetch the image from there.

myColumn.ImageAspectName = "Image";

Another way to do it is using the ImageGetter of a row. This is more efficient if several of your objects use the same image, because you can fetch the image from anywhere you want or even use the assigned ImageList from the OLV by just returning an index.

indexColumn.ImageGetter += delegate(object rowObject) {
    // this would essentially be the same as using the ImageAspectName
    return ((Item)rowObject).Image;
};

As pointed out, the ImageGetter can also return an index with respect to the ObjectListView's assigned ImageList:

indexColumn.ImageGetter += delegate(object rowObject) {
    int imageListIndex = 0;

    // some logic here
    // decide which image to use based on rowObject properties or any other criteria

    return imageListIndex;
};

This would be the way to reuse images for multiple objects.

这篇关于ObjectListView图像添加到项目/对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆