Included below is the source to a simple WPF application which uses a TreeView. Here's what it looks like when run:

enter image description here

Note however that if I click on one of the TreeViewItem headers:

enter image description here

the text in the TreeViewItem is no longer visible.

What's a good way to fix this so that the text remains visible when the header is selected?

As you can see, the code is primarily in C# so C#-based answers are preferred, but XAML is welcome too; I'll just convert it to C#.


UPDATE

If I set the Foreground of the TextBlock explitly to Black as suggested in an answer below:

{
    var tree_view_item = new TreeViewItem() { Header = "abc" };

    tree_view_item.Items.Add(new ScrollViewer() { Content = new TextBlock() { Text = "123", Foreground = new SolidColorBrush(Colors.Black) } });

    tree_view.Items.Add(tree_view_item);
}

it does indeed appear to help:

enter image description here

However, if I then select the TextBlock, the textblock is shown as black on blue which is a little awkward:

enter image description here

Is there a way to also change the background color used when the item is highlighted?


MainWindow.xaml:

<Window x:Class="TreeViewItemHighlightColor.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TreeViewItemHighlightColor"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>

    </Grid>
</Window>

MainWindow.xaml.cs:

using System.Windows;
using System.Windows.Controls;

namespace TreeViewItemHighlightColor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var tree_view = new TreeView();

            {
                var tree_view_item = new TreeViewItem() { Header = "abc" };

                tree_view_item.Items.Add(new ScrollViewer() { Content = new TextBlock() { Text = "123" } });

                tree_view.Items.Add(tree_view_item);
            }

            {
                var tree_view_item = new TreeViewItem() { Header = "bcd" };

                tree_view_item.Items.Add(new ScrollViewer() { Content = new TextBlock() { Text = "234" } });

                tree_view.Items.Add(tree_view_item);
            }

            {
                var tree_view_item = new TreeViewItem() { Header = "cde" };

                tree_view_item.Items.Add(new ScrollViewer() { Content = new TextBlock() { Text = "345" } });

                tree_view.Items.Add(tree_view_item);
            }

            Content = tree_view;
        }
    }
}

1 Answers

1
TheXDS On

There's a chance that the Foreground property of the TextBlock is being inherited and altered by the state of the TreeViewItem.

Set the Foreground property of the TextBlock to black. That way, the TreeViewItem will not override it.