СhiliРepper
СhiliРepper

Reputation: 125

How to set an icon for each object in TreeListView

I have some icons which should be displayed in the first column next to the name of RowObject. Different pictures related to different type of obects. This part of code creates columns with objects that I need.

    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;

Upvotes: 3

Views: 1818

Answers (2)

Rev
Rev

Reputation: 6102

You have to create and assign an ImageList containing the images you want to use like this for example

descriptionTView.SmallImageList = imageList1;

Then you can implement an ImageGetter for the desired column

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

You can also return the name of the image in the list, instead of the index.

You may have to set

descriptionTView.OwnerDraw = true;

An alternative would be to get the image directly from the row object using the ImageAspectName

column1.ImageAspectName = "MyImage"; // Assuming your object has a property `public BitMap MyImage;`

Upvotes: 4

Hasan Akg&#252;n
Hasan Akg&#252;n

Reputation: 465

you need to add a ImageList to your TreeView, after that you can set a node's with ImageKey attribute.

Upvotes: 0

Related Questions