DevExpress的TreeList实现自定义节点NodeCell的背景颜色和前景色
場(chǎng)景
DevExpress的TreeList怎樣設(shè)置數(shù)據(jù)源使其顯示成單列樹形結(jié)構(gòu):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102742426
在上面設(shè)置TreeList的數(shù)據(jù)源并設(shè)置其為樹形結(jié)構(gòu)后,如果想要自定義NodeCell的背景色和前景色。
效果如下
?
注:
博客主頁(yè):
https://blog.csdn.net/badao_liumang_qizhi
關(guān)注公眾號(hào)
霸道的程序猿
獲取編程相關(guān)電子書、教程推送與免費(fèi)下載。
實(shí)現(xiàn)
通過重寫treelist的CustomDrawNodeCell事件來實(shí)現(xiàn)
treeList.CustomDrawNodeCell -= treeList_CustomDrawNodeCell; treeList.CustomDrawNodeCell += treeList_CustomDrawNodeCell;然后在重寫的事件中
??????? private static void treeList_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e){//確定列的名字不為空if (e.Column.Name.ToString() != null){Color color = new Color();//獲取要設(shè)置的顏色--通過e.CellValue獲取當(dāng)前node節(jié)點(diǎn)的值,然后在字典映射中獲取要設(shè)置的color對(duì)象Global.Instance.CurveComColorMapping.TryGetValue(e.CellValue.ToString(), out color);//設(shè)置背景色e.Appearance.BackColor = color;//設(shè)置開啟背景色e.Appearance.Options.UseBackColor = true;//設(shè)置前景色e.Appearance.ForeColor = Color.White;//設(shè)置開啟前景色e.Appearance.Options.UseForeColor = true;}}注:
上面的是通過e.CellValue來獲取節(jié)點(diǎn)的值,然后通過提前設(shè)置好的節(jié)點(diǎn)與顏色的映射來獲取要設(shè)置的顏色。
鍵值對(duì)的映射定義如下
private Dictionary<string, System.Drawing.Color> _curveComColorMapping = new Dictionary<string,System.Drawing.Color>();這樣就能實(shí)現(xiàn)根據(jù)配置好的節(jié)點(diǎn)與顏色的對(duì)應(yīng)關(guān)系將節(jié)點(diǎn)設(shè)置為不同的顏色。
總結(jié)
以上是生活随笔為你收集整理的DevExpress的TreeList实现自定义节点NodeCell的背景颜色和前景色的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Winform中设置ZedGraph的多
- 下一篇: Winform中设置ZedGraph在生