I have a simple tree view with all colors by default. When you click on an element, it is highlighted, and the background turns blue. Still. I am trying to do the same in code so that I can have the treeview element of my choice highlighted in blue. The only property I see, I found that the background change is ".IsSelected". But when I set this value to true, the treeview element is highlighted in white. I do not understand. I do not set the highlight color for anything, so why choose a different highlight color than when you select the same item with the mouse? Is “highlighting” a different property than “select”, and if so, what is the name of this property?
Thanks.
Edit: adding code (My apologies, it was so easy, so I didn’t think anyone would want to see the code)
Xaml: (and yes, this is all the code. Just connect it to the project and it will start.)
<Window x:Class="TestTreeView.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Tree View Icon" ShowInTaskbar="false" Name ="MyTestTreeView" Background="Beige" Width="500" Height ="500" > <StackPanel Name="stackpanel"> <Button Name="AddNode" Click="btnClick"> AddNode </Button> <ScrollViewer HorizontalScrollBarVisibility="auto" VerticalScrollBarVisibility="hidden" Name="scrollViewer"> <TreeView Name="treeView" BorderThickness="0"> <TreeView.Resources> <SolidColorBrush Color="Red" x:Key="{x:Static SystemColors.HighlightBrushKey}"/> </TreeView.Resources> </TreeView> </ScrollViewer> </StackPanel>
Code behind:
using System.Windows.Shapes; using System.IO; namespace TestTreeView { public partial class Window1 : System.Windows.Window { public Window1() { InitializeComponent(); } public void btnClick(object sender, RoutedEventArgs e) { TreeViewItem n1 = new TreeViewItem(); n1.Header = "Top Node"; n1.IsSelected = true; n1.Focus(); treeView.Items.Add(n1); } } }
source share