c# - 如何为TreeListView中的每个对象设置一个图标

标签 c# objectlistview treelistview

我有一些图标应该显示在 RowObject 名称旁边的第一列中。 不同的图片与不同类型的对象相关。 这部分代码使用我需要的对象创建列。

    this.descriptionTView = new BrightIdeasSoftware.TreeListView();
    this.descriptionTView.CanExpandGetter = x =>
    {
    if ( ( x as ITreeViewItem ).Items != null )
       {
       return ( x as ITreeViewItem ).Items.Length > 0;
       }
    else
       {
       return false;
       }
    };                

    this.descriptionTView.ChildrenGetter = x => ( x as ITreeViewItem ).Items;

    var column1 = new BrightIdeasSoftware.OLVColumn( "", "Part0" );
    column1.AspectGetter = x => ( x as ITreeViewItem ).Part0;
    column1.IsEditable = true;

    var column2 = new BrightIdeasSoftware.OLVColumn( "", "Part1" );
    column2.AspectGetter = x => ( x as ITreeViewItem ).Part1;
    column1.IsEditable = true;
    column2.IsEditable = true;

    this.descriptionTView.Columns.Add( column1 );
    this.descriptionTView.Columns.Add( column2 );
    private List<ITreeViewItem> itemsList;
    descriptionTView.Roots = itemsList;

最佳答案

例如,您必须创建并分配一个包含您要使用的图像的 ImageList

descriptionTView.SmallImageList = imageList1;

然后您可以为所需的列实现一个 ImageGetter

column1.ImageGetter = delegate(object rowObject) {
    // decide depending on the rowObject which image to return
    return 0;
};

您还可以返回列表中图像的名称,而不是索引。

你可能需要设置

descriptionTView.OwnerDraw = true;

另一种方法是使用 ImageAspectName

直接从行对象获取图像
column1.ImageAspectName = "MyImage"; // Assuming your object has a property `public BitMap MyImage;`

关于c# - 如何为TreeListView中的每个对象设置一个图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24313897/

相关文章:

c# - 反序列化多维 JSON 字符串

java - JFace:为特定节点扩展更多级别

c# - 我可以将任意数量的命名参数传递给 C# 中的函数吗?

c# - 在 SQL Server 中根据日期自动发送电子邮件

c# - 在 ObjectListView 中应用默认排序列

c# - 在 ObjectListView 中保存和恢复选择

c# - 在objectListView中构建treeListView的模型

C# objectlistview 单元格编辑器中 undefined offset

c# - 不同 ASP.NET 缓存选项的优缺点