M
M
Michael2015-04-24 18:53:31
WPF
Michael, 2015-04-24 18:53:31

WPF. XAML. The MouseUp event for LMB does not work. How to fix?

There is a style for the button. The button itself is two rectangles (on one background color, on the other image with transparency).

<Rectangle Name="RectangleBackground" 
        HorizontalAlignment="Stretch" 
        Fill="#D01600" 
        Opacity="0"/>
<Rectangle Name="Rectangle" 
        HorizontalAlignment="Stretch" 
        Margin="5" 
        Fill="{TemplateBinding Control.Background}" 
        Opacity="1"/>

All main events work (MouseEnter, MouseLeave, MouseDown). Here is an example of one of them:
<EventTrigger RoutedEvent="MouseDown" SourceName="Rectangle">
        <EventTrigger.Actions>
                <BeginStoryboard>
                        <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="RectangleBackground"
                                        Storyboard.TargetProperty="Opacity"
                                        From="1" To="0.5" Duration="0:0:0.15"/>
                        </Storyboard>
                </BeginStoryboard>
        </EventTrigger.Actions>
</EventTrigger>

But when processing the MouseUp event, it only affects the RMB and MMB. The question is how to catch the LMB release event? And why does MouseUp work differently than I expect? MSDN says that the event should occur when any mouse button is released.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2015-04-25
@gmikhail94

As I was told, the problem is in SourceName if used with PreviewMouseUp or PreviewMouseLeftButtonUp. Works without it:

<EventTrigger RoutedEvent="PreviewMouseUp">
        <EventTrigger.Actions>
                <BeginStoryboard>
                        <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="RectangleBackground"
                                        Storyboard.TargetProperty="Opacity"
                                        From="1" To="0.5" Duration="0:0:0.15"/>
                        </Storyboard>
                </BeginStoryboard>
        </EventTrigger.Actions>
</EventTrigger>

PS: It was not possible to make MouseUp work or at least find out why it does not work.

S
Sumor, 2015-04-24
@Sumor

Perhaps somewhere up the visual tree there is a Click handling that "eats" the LeftButtonUp.
Try using PreviewMouseLeftButtonUp.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question