Reputation: 2613
I have this style:
<Style TargetType="{x:Type Expander}">
<Setter Property="BorderBrush" Value="LightGray"/>
<Setter Property="Margin" Value="5"/>
<EventSetter Event="Mouse.MouseEnter" Handler="ExpanderMouseEnter"/>
<EventSetter Event="Mouse.MouseLeave" Handler="ExpanderMouseLeave"/>
</Style>
When I add an expander as a child to an expander and mouseover the child expander, both of the expanders gets colored gray. How can I set the e.handled = true to the event to stop it from bubbling up?
EDIT: OK so I had to edit the style to call a code-behind method. Here are the methods:
Private Sub ExpanderMouseEnter(ByVal sender As Expander, ByVal e As MouseEventArgs)
sender.Background = Brushes.LightGray
e.Handled = True
End Sub
Private Sub ExpanderMouseLeave(ByVal sender As Expander, ByVal e As MouseEventArgs)
sender.Background = Brushes.White
e.Handled = True
End Sub
But nothing changes. What's the problem?
Upvotes: 2
Views: 1255
Reputation: 19885
Ideally writing a Trigger
to change the background with IsMouseDirectlyOver
flag should work as this flag is intended for UIElements to activate triggers ONLY on which mouse is directly over.
But it seems to fail for expanders because inner expander is part of outer expander's visual tree.
I believe what you might consider is to have IsMouseOver
trigger on the Header
element of expanders. They are mutually exclusive of each other's visual tree. So when you mouse over the headers the expanders will get their characteristic background color.
Upvotes: 2
Reputation: 3285
I think the real problem is that a MouseLeave
event is not fired when the mouse moves from the outer expander into the inner expander. As a result, the IsMouseOver
property used in your Triggers
remains set on both (as in your original question). So even if you could set the Handled
property to True
as the mouse enters the second expander, your Trigger
would still be active on the outer control, and the background set.
Upvotes: 0
Reputation: 437336
It looks like this is not possible using no code-behind, because neither Trigger
nor EventTrigger
(which you could use alternatively in this scenario) expose any relevant functionality.
Unless you can work around the issue (e.g. by only applying the style, or parts of it, to descendant expanders), you will have to implement an event handler in code-behind and set the Handled
property from there.
Upvotes: 0