tags:

views:

86

answers:

1

I have the following situation:

A stackpanel contains a number of elements, including some that are contained in a GroupBox. So something like this:

<StackPanel x:Name="stackpanel" Background="White">
    <TextBlock Text="TextBlock"/>
    <TextBlock Text="Another TextBlock"/>
    <!--plus a load of other elements and controls-->

    <GroupBox Header="GroupBoxHeader">
        <TextBlock Text="Text inside GroupBox"/>
    </GroupBox>

</StackPanel>

I want a MouseDown in the stackpanel to trigger some Storyboard, so I've added an EventTrigger, like this:

<EventTrigger RoutedEvent="Mouse.MouseDown" SourceName="stackpanel">
    <BeginStoryboard Storyboard="{StaticResource OnMouseDown1}"/>
</EventTrigger>

This is almost right, but the thing is - I don't want the MouseDown to be picked up by the GroupBox's header or border, only by its content. In other words, I want the Storyboard to begin when someone does a mousedown on anything inside the StackPanel, except GroupBox headers and borders.

Is there some way of doing this? (I've tried setting e.Handled to true on the GroupBox, but then its content doesn't pick up the mousedown anymore either.)

+1  A: 

You're on the right track. In the GroupBox handler, set e.Handled to true only if it's not one of the options you want to trigger. A simple trick for this is that sender is the object where you assigned the handler, but e.Source is where the event actually occured. For example, if I do this:

    private void GroupBox_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.Source.Equals(sender))
        {
            //It comes from the groupbox, including the header
            e.Handled = true;
        }
        else
        {
            e.Handled = false;
        }
    }

The event only reaches the parent if it came from one of the GroupBox's children. The ones that originate with the GroupBox itself (including the header) are suppressed.

Ben Von Handorf
Exactly what I was looking for. Thanks!
cfouche