Changing Foreground of a TreeViewItem upon selection

两盒软妹~` 提交于 2019-12-06 01:31:40

This is because you completely rewrite the template, and you do not write anything instead. Just to set the triggers, not necessarily to do them in the template, you can just set them in Style. Template is typically set to change the elements in the visual tree. Try this example:

<Window.Resources>
    <Style TargetType="{x:Type TreeViewItem}">
        <Style.Resources>
            <!-- Set Highlight Background color -->
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Black" />
        </Style.Resources>

        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <!-- Set Foreground color -->
                <Setter Property="Foreground" Value="White" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <TreeView>
        <TreeViewItem Header="Root">
            <TreeViewItem Header="Child1" />
            <TreeViewItem Header="Child2" />
            <TreeViewItem Header="Child3" />
            <TreeViewItem Header="Child4" />
        </TreeViewItem>
    </TreeView>
</Grid>

For more information, please see:

Styling and Templating on MSDN

Example of TreeView Style/Template on MSDN

EDIT

Try this:

public TreeViewItem newItem = new TreeViewItem() //Child Node
{
    Header = new StackPanel 
    {
        Orientation = Orientation.Horizontal,

        Children =
        {
            new Border 
            {
                Width = 12,
                Height = 14,

                Background = Brushes.Blue,
                BorderThickness = new Thickness(1.0),
                BorderBrush = Brushes.Black
            },

            new Label 
            {
                Content = "Node1",
                Foreground = Brushes.Black,
            }
        }
    }
};

private void AddItem_Click(object sender, RoutedEventArgs e)
{
    // Set Selected handler on Selected event
    newItem.Selected += new RoutedEventHandler(newItem_Selected);

    // Set Unselected handler on Unselected event
    newItem.Unselected += new RoutedEventHandler(newItem_Unselected);

    // Add your item
    MyTreeView.Items.Add(newItem);
}

// Set the black color for foreground
private void newItem_Unselected(object sender, RoutedEventArgs e) 
{
    TreeViewItem MyTreeViewItem = sender as TreeViewItem;
    StackPanel MyStackPanel = MyTreeViewItem.Header as StackPanel;
    Label MyLabel = MyStackPanel.Children[1] as Label;

    MyLabel.Foreground = Brushes.Black;
}

// Set the white color for foreground
private void newItem_Selected(object sender, RoutedEventArgs e)
{
    TreeViewItem MyTreeViewItem = sender as TreeViewItem;
    StackPanel MyStackPanel = MyTreeViewItem.Header as StackPanel;
    Label MyLabel = MyStackPanel.Children[1] as Label;

    MyLabel.Foreground = Brushes.White;         
}

Note: This code can be shortened and made easier if you use a template for TreeViewItem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!