How do you allocate a TreeViewItem?

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); } } } 
0
source share
3 answers

The TreeviewItem.Focus () method will help solve your problem. In the code you skipped to set the ItemContainerStyle and you need to focus the TreeViewItem after adding it to the TreeView, as shown below

 <Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Style x:Key="myLBStyle" TargetType="{x:Type TreeViewItem}"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green" /> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Red" /> </Style.Resources> </Style> </Window.Resources> <Grid> <TreeView Name="Treeview1" ItemContainerStyle="{StaticResource myLBStyle}" /> <Button Content="Select" Width="300" Height="30" Click="Button_Click_2" /> </Grid> 

 TreeViewItem n1 = new TreeViewItem(); n1.Header = "Top Node"; n1.IsSelected = true; Treeview1.Items.Add(n1); n1.Focus(); 
+1
source

Use the Focus () method for TreeViewItem.

0
source

I believe that for the most convenient approach, you will need to set SelectedItem or SelectedValue to TreeView as one of the items of interest to your ItemsSource for the control. If you use bindings, this should be very easy to do - just update the corresponding property in the ViewModel.

This is what you need to do if you are trying to simulate a user selecting an item from your list. Let me know if I'm wrong or you need more recommendations, but first you will need more code / examples.

0
source

Source: https://habr.com/ru/post/1205682/