您可以在創建節點時直接設置展開/摺疊圖標。例如:
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;
或者,如果你想這樣做的格式更一般(在這裏您不需要每次創建節點時指定的圖像),你可以處理NodeFormatting事件是這樣的:
//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;
}
注意:我相信NodeFormatting事件是比較新的,所以你需要最近版本的Telerik來使用它。如果您在使用此代碼時遇到問題,請確保您正在運行最新版本的控件。
當你說你想要動態地填充樹時,你想擴展/摺疊圖標爲不同的行不同?或者你只是計劃通過代碼添加節點,但可以爲所有節點添加相同的圖標? –