WPF TreeViewItem Context Menu Unhighlights Item

Posted by Snea on Stack Overflow See other posts from Stack Overflow or by Snea
Published on 2010-06-12T22:17:11Z Indexed on 2010/06/12 22:22 UTC
Read the original article Hit count: 800

Filed under:
|
|

I have been having problems with this for some time now, and have come up with some less-than-desirable solutions. The problem is that when a TreeViewItem's context menu is opened, the TreeViewItem is greyed out. Is it possible for a TreeViewItem to stay highlighted while its ContextMenu is open?

The problem with the TreeViewItem greying out, is that it gives no relation to the context menu and the TreeViewItem, and it looks ugly.

Generally, the code I use for setting a context menu is this. Sometimes the context menu will be generated by the code with a PreviewRightMouseButtonDown EventSetter, but it doesn't make a difference.

<TreeView>
    <TreeView.Resources>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu>
                        <MenuItem Header="Menu Item 1" />
                        <MenuItem Header="Menu Item 2" />
                    </ContextMenu>
                </Setter.Value>
            </Setter>
        </Style>
    </TreeView.Resources>
    <TreeViewItem Header="Item 1">
        <TreeViewItem Header="Sub-Item 1"/>
    </TreeViewItem>
    <TreeViewItem Header="Item 2"></TreeViewItem>
</TreeView>

So far the only solution I have found is to override the "grey" unfocused color with the focused color, but then the TreeView never seems unfocused, such as when another control is clicked on. I have had problems with ListViews as well.

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf