Pete
Pete

Reputation: 10871

Is there any way to strongly type a controls Tag property?

I have a TreeView control which displays two things:

1) Folder
2) Item

Where folders contain items. And the root folder contains all subfolders as well as items that do not belong to a folder.

I have a Folder type associated with the nodes that represent folders and an Item type associated with the nodes that represent items.

Now, the user can select any node, and perform different actions. I need to be able to distinguish between the types stored in the Tag property. If this is not possible, what are my options for getting around this?

Upvotes: 3

Views: 134

Answers (2)

Alex Aza
Alex Aza

Reputation: 78457

You can easily make a typed tag if needed:

public class MyTreeNode<T> : TreeNode
{
    public T TypedTag { get; set; }
}

Upvotes: 2

tyranid
tyranid

Reputation: 13318

Use the 'as' and or 'is' operator? e.g.

if(node.Tag is Folder)
{
     Folder f = node.Tag as Folder;
}
else if (node.Tag is Item)
{
     ...
}

Upvotes: 2

Related Questions