views:

67

answers:

1

Hello. Here's what I'm trying to do:

 <Style x:Key="TreeViewItemStyle">
            <Setter Property="TreeViewItem.ContextMenu" Value="{StaticResource ContextMenu}" />
            <Style.Triggers>
                <Trigger Property="TreeViewItem.ContextMenu.IsOpen" Value="True">
                    <Setter Property="TreeViewItem.BitmapEffect">
                        <Setter.Value>
                            <OuterGlowBitmapEffect GlowColor="Yellow" GlowSize="2"/>
                        </Setter.Value>
                    </Setter>
                </Trigger>
        </Style> 

But it is obviously not working because Property="TreeViewItem.ContextMenu.IsOpen" is not recognized. Any suggestions to what I need to change?

A: 

You can bind to the IsOpened property of the context menu using a DataTrigger:

<DataTrigger Binding="{Binding ContextMenu.IsOpen, RelativeSource={RelativeSource Self}}" Value="True">
    <Setter Property="Background" Value="Green"/>
</DataTrigger>

Unfortunately, since all of the items in TreeView share the same ContextMenu, that will highlight all of them at once. There doesn't seem to be a property that lets you find out which FrameworkElement opened the ContextMenu.

You could handle the ContextMenuOpening and ContextMenuClosing events on the TreeViewItem, since those will bubble up from the control that handled the click and pass through the right TreeViewItem. If you want to do it in XAML, you could use an EventTrigger to start a one-frame animation that changes your property. The cleanest option may be to write an attached behavior that handles the ContextMenuOpening and ContextMenuClosing events and sets an attached property to true when the context menu is open.

Quartermeister