How do I change the expand/collapse icon on a Telerik winform Treeview?
I'm trying to customise the expand/collapse icon on a Telerik Winform TreeView control.
If I populate a TreeView control with the PropertyBuilder, I can see Telerik.WinControls.UI.ExpandCollapseElements which I can modify to change the expand/collapse icons. However I want to populate my tree dynamically.
I have exam开发者_如何学Pythonined modifying the theme but I don't see ExpandCollapseElements anywhere in Visual Style Builder, nor do I see any way to modify these icons at the RadTreeView end of the object model.
Screenshots say it can be done, but what am I missing?
You can set the expand/collapse icons directly when you are creating the nodes. For example:
Image imageToUseForExpand = /* get this image from somewhere */;
Image imageToUseForCollapse = /* get this image from somewhere */;
RadTreeNode item = new RadTreeNode("Node with custom icons");
//NOTE: You need to add the node to the treeview before working
// with the TreeViewElement property (otherwise it will be null)
radTreeView1.Nodes.Add(item);
//Set the expand and collapse images to whatever you want
item.TreeViewElement.ExpandImage = imageToUseForExpand;
item.TreeViewElement.CollapseImage = imageToUseForCollapse;
Or if you would like to do the formatting more generically (where you don't need to specify the images every time you create a node), you could handle the NodeFormatting event like this:
//Either register the even in code (like this) or via the Designer
radTreeView1.NodeFormatting += radTreeView1_NodeFormatting;
//Then in the event handler, set the appropriate image
private void radTreeView1_NodeFormatting(object sender,
TreeNodeFormattingEventArgs e)
{
//See whether the node is currently expanded and set the image accordingly
if (e.Node.Expanded)
e.NodeElement.ExpanderElement.SignImage = imageToUseForExpand;
else
e.NodeElement.ExpanderElement.SignImage = imageToUseForCollapse;
}
Note: I believe the NodeFormatting event is relatively new so you'll need a recent version of Telerik to use it. If you have trouble with this code, make sure you're running the latest version of the controls.
To globally change the expand and collapse images then do the following:
Image imageToUseForExpand = /* get this image from somewhere */;
Image imageToUseForCollapse = /* get this image from somewhere */;
radTreeView1.TreeViewElement.ExpandImage = imageToUseForExpand;
radTreeView1.TreeViewElement.CollapseImage = imageToUseForCollapse;
I ended up saving the theme I was using as XML, hand editing the XML for that theme, reimporting the XML and saving it as .tssp. I was fortunate that the image I wanted was part of another theme.
This approach probably works best in my situation because we will need theme support to make things look good in all the platforms we support.
The other approaches suggested look good; I haven't tried any of them.
精彩评论