1

I have the following pieces of code which should create a tree. Each item of that tree should change its background when it is hovered.

XAML:

<TreeView Name="treeView">
    <TreeViewItem Header="Item 1" IsExpanded="True">
        <TreeViewItem Header="Item 1.1" IsExpanded="True">
            <TreeViewItem Header="Item 1.1.1"/>
            <TreeViewItem Header="Item 1.1.1"/>
        </TreeViewItem>
        <TreeViewItem Header="Item 1.2"/>
    </TreeViewItem>
</TreeView>

C# Code behind:

class MainWindow : Window
{
    // ...

    private void SetEventHandlersOn(ItemCollection items)
    {
        foreach (TreeViewItem item in items)
        {
            item.MouseEnter += ItemMouseEnter;
            item.MouseLeave += ItemMouseLeave;
            SetEventHandlersOn(item.Items);
        }
    }

    private void ItemMouseEnter(object sender, MouseEventArgs e)
    {
        var treeViewItem = sender as TreeViewItem;
        treeViewItem.Background = Brushes.LimeGreen;
        e.Handled = true;
    }

    private void ItemMouseLeave(object sender, MouseEventArgs e)
    {
        var treeViewItem = sender as TreeViewItem;
        treeViewItem.Background = Brushes.White;
        e.Handled = true;
    }
}

Instead of doing what I need the tree appears differently. When I hover an item, it becomes highlighted, but its parent items are also highlighted for some reason.

enter image description here

How can I solve the problem?

UPD1 I see that my problem is very similar to the problem described here but I haven't understood yet how to apply the solution.

Community
  • 1
  • 1
Igor Soloydenko
  • 11,067
  • 11
  • 47
  • 90
  • Essentially you need to have an IsMouseOver style trigger for the TreeViewItem. Here's a link: http://stackoverflow.com/questions/1131161/how-can-i-make-wpf-trigger-for-ismouseover-on-treeviewitem-not-affect-all-parent – Josh Feb 08 '12 at 18:35
  • @Josh , to be honest I should say that I haven't tried that yet. I will do it. It may help. But do you have any idea about a solution without Triggers and styling? It's interesting how can I prevent the event to be raised on parent items. – Igor Soloydenko Feb 08 '12 at 18:36
  • It's just the standard behavior of WPF although I'm not sure why. If you follow that StackOverflow link further and click on the link in the first answer, you'll see you have to perform a little more work to prevent IsMouseOver over multiple items by watching the PART_Header. – Josh Feb 08 '12 at 18:40
  • Thank you! This standard behavior seems to be strange. At least it looks so for me. WinForms world has completely other rules of the game. :) – Igor Soloydenko Feb 08 '12 at 18:48
  • 1
    Welcome to WPF :). WPF has very weird behavior sometimes, especially with TreeViews. It makes the difficult stuff easy, and the easy stuff difficult. – Josh Feb 08 '12 at 19:03

1 Answers1

4

you should check for e.Handled state in your handler like this:

private void ItemMouseEnter(object sender, MouseEventArgs e)
{
    if (!e.Handled)
    {
        var treeViewItem = sender as TreeViewItem;
        treeViewItem.Background = Brushes.LimeGreen;
        e.Handled = true;
    }
}

then it will be handled only once.

Victorian
  • 41
  • 3