poy
poy

Reputation: 10507

TreeView highlight color in .NET 2.0

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?

Upvotes: 0

Views: 694

Answers (2)

Garry
Garry

Reputation: 5074

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());
}

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941327

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.

Upvotes: 2

Related Questions