如何在TreeListView中为每个对象设置图标

本文关键字:对象 设置 图标 TreeListView | 更新日期: 2023-09-27 18:09:18

我有一些图标,应该显示在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;

如何在TreeListView中为每个对象设置图标

您必须创建并分配一个包含您想要使用的图像的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;`

你需要添加一个ImageList到你的TreeView,之后你可以设置一个节点的ImageKey属性。