Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何访问ImageGetterDelegate内部的OLVColumn发送器_C#_.net_Winforms_Objectlistview - Fatal编程技术网

C# 如何访问ImageGetterDelegate内部的OLVColumn发送器

C# 如何访问ImageGetterDelegate内部的OLVColumn发送器,c#,.net,winforms,objectlistview,C#,.net,Winforms,Objectlistview,使用ImageGetterDelegate在ObjectListView中创建动态生成的列时出现问题,ImageGetterDelegate用于设置该字段中显示的图像 尝试执行以下操作时: myOLVColumn.ImageGetter = delegate(Object x) { /*can't access myOLVColumn here*/ return getImage(x); }; 我无法访问代理中的原始列对象(我需要确定要显示的图像): 也许解决方案是使用一些我还找不到的事件来代

使用ImageGetterDelegate在ObjectListView中创建动态生成的列时出现问题,ImageGetterDelegate用于设置该字段中显示的图像

尝试执行以下操作时:

myOLVColumn.ImageGetter = delegate(Object x) { /*can't access myOLVColumn here*/ return getImage(x); };
我无法访问代理中的原始列对象(我需要确定要显示的图像):

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

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

如果是,怎么做

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

 public delegate object ImageGetterDelegate(object rowObject);


我自己找到了解决办法

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

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对象,因为它正由委托对象指向

这个技巧对基本类型不起作用,因为对该变量的任何更改都会影响所有委托,因为它们不是指向对象的指针,所以请确保使用扭曲器(Int32而不是int,String而不是String等等)


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

您想对多个ImageGetter使用一个处理程序,并确定它是从哪个列触发的?如果不按照您的建议扩展ImageGetterDelegate,我认为这是不可能的。为什么不像您的示例中那样为每一列使用一个ImageGetter委托呢?或者我遗漏了什么?它不是同一个委托,因为委托是一个对象,它会创建几个委托(该代码在for循环中),但它使用相同的“代码段”来生成所有ImageGetterDelegates,所以在某种程度上是的,我想要。我不知道将有多少列,所以我必须动态生成整个列,我不能硬编码每个ImageGetterDelegateAh好的,这就清除了它。正如您所指出的,这正是常用的“对象发送器”的用途。所以我可能会选择源代码并扩展ImageGetterDelegate签名。
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);
    };
    //...
}