轉載:
https://documentation.devexpress.com/#WindowsForms/DevExpressXtraTreeListTreeList_CustomDrawNodeButtontopic
1.修改節點圖標
(1)拖入TreeList控件(這里如何加載數據就不多說了哈)和ImageLIst控件
a.給ImageList添加圖像
b.修改TreeList控件的屬性
(2).在屬性欄找到TreeList控件的CustomDrawNodeImages事件,並添加以下代碼
private void treeList1_CustomDrawNodeImages(object sender, DevExpress.XtraTreeList.CustomDrawNodeImagesEventArgs e) { if (e.Node.Nodes.Count > 0) { if (e.Node.Expanded) { e.SelectImageIndex = 2; return; } e.SelectImageIndex = 1; } else { e.SelectImageIndex = 0; } }
(3)完成
2.修改按鈕樣式
在TreeList控件屬性欄找到CustomDrawNodeButton事件,並添加以下代碼
private void treeList1_CustomDrawNodeButton(object sender, DevExpress.XtraTreeList.CustomDrawNodeButtonEventArgs e) { Rectangle rect = Rectangle.Inflate(e.Bounds, 0, -2); // painting background Brush backBrush = new LinearGradientBrush(rect, Color.Blue, Color.LightSkyBlue, LinearGradientMode.ForwardDiagonal); //填充指定矩形內部 e.Graphics.FillRectangle(backBrush, rect); //繪畫3D邊框 ControlPaint.DrawBorder3D(e.Graphics, rect, Border3DStyle.RaisedOuter); //要顯示的圖畫 string displayCharacter = e.Expanded ? "-" : "+"; //水平,垂直居中 StringFormat outCharacterFormat = new StringFormat(); outCharacterFormat.Alignment = StringAlignment.Center; outCharacterFormat.LineAlignment = StringAlignment.Center; //繪畫圖標 e.Graphics.DrawString(displayCharacter, new Font("Verdana", 8, FontStyle.Bold), new SolidBrush(Color.White), rect, outCharacterFormat); // 禁止默認的圖標顯示 e.Handled = true; }