TreeView highlight color in .NET 2.0

2019-08-20 03:34发布

I'm trying to change the TreeView's default highlight color. I would have expected something like DefaultSelectionBackground like I do in a DataGridView, but I can't find anything.

Is this just another limitation of the TreeView, or am I missing something?

2条回答
孤傲高冷的网名
2楼-- · 2019-08-20 04:03

All standard Windows controls, like TreeView, pay attention to the theme colors selected by the user. That's an asset, windows are recognizable and familiar, even if the user has never used a program before. The default highlight color is white on blue, standard for every control. And of course customizable by the user, TreeView automatically follows suit.

You can force your own color preference on the user if you really want to. Set the DrawMode property to OwnerDrawText and implement the DrawNode event to draw yourself. There's a good example for it in the MSDN Library article for DrawNode.

查看更多
放荡不羁爱自由
3楼-- · 2019-08-20 04:03

HTML

<asp:TreeView runat="server"
              ID="tvMyTreeView"
              OnTreeNodeDataBound="tvMyTreeView_TreeNodeDataBound"/>

Code behind

protected void tvMyTreeView_TreeNodeDataBound(object aSender, TreeNodeEventArgs anEvent)
{
    DataRowView dr = (DataRowView)anEvent.Node.DataItem;
    anEvent.Node.Style.Add("color", dr["COLOR"].ToString());
}
查看更多
登录 后发表回答