0

在我的 ObjectListView 中使用 ImageGetterDelegate 创建动态生成的列时,我遇到了一个问题,该 ImageGetterDelegate 用于设置该字段中显示的图像。

尝试执行以下操作时:

myOLVColumn.ImageGetter = delegate(Object x) { /*can't access myOLVColumn here*/ return getImage(x); };

我无法访问委托中的原始 Column 对象(我需要确定要显示哪个图像):

也许解决方案是使用一些我还找不到的事件而不是委托。

有谁知道是否可以访问委托内的列对象或以任何其他动态方式基于列属性设置字段图像?

如果是这样,如何?

如果不可能,那么如果他们改变了,那就太好了:

 public delegate object ImageGetterDelegate(object rowObject);

public delegate object ImageGetterDelegate(object rowObject, object sender);
4

1 回答 1

1

我自己找到了解决方案!

我可以在委托定义之前创建一个对象,然后在委托内部使用该对象没有问题!它将被保存为委托定义中的指针。

foreach (DirectoryInfo dir in directoryList.GetDirectories())
{
    BrightIdeasSoftware.OLVColumn myOLVColumn = new BrightIdeasSoftware.OLVColumn();
    myOLVColumn.ImageGetter = delegate(Object x) {
        /*I CAN access myOLVColumn here, 
as far as I don't mess with that object in the other code, 
and even if the for loop changes myOLVColumn, as far as it makes a new one and don't destroy it
the delegate will still have the pointer to the object the variable pointed to 
during the generation of the delegate*/
        return getImage(x, myOLVColumn);
    };
    //...
}

垃圾收集器不会删除该 myOLVColumn 对象,因为它是由委托对象指向的。

此技巧不适用于原始类型,因为对该 var 的任何更改都会影响所有委托,因为它们不是指向对象的指针,因此请确保使用变形器(Int32 代替 int,String 代替 string 等等上)。

无论如何,这有点棘手,所以如果扩展委托定义可能会很好,当然前提是它不会过多地搞乱 ObjectListView 库的内部工作。

于 2013-11-20T08:27:43.783 回答