Home > Software engineering >  Why does the IsPressedChanged event not work with an interaction trigger but other events do?
Why does the IsPressedChanged event not work with an interaction trigger but other events do?

Time:05-14

Edit: To make it short: as AFigmentOfMyImagination pointed out in his answer, there exists no IsPressedChanged event. However, here is the original question:

I have a button and I want a command to be executed both when the button is pressed and when it is released. The standard command binding of buttons can only call the command either when the button is pressed OR released so I figured I might achieve what I want using an interaction trigger on the IsPressedChanged event like so:

<Button xmlns:i="http://schemas.microsoft.com/xaml/behaviors">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="IsPressedChanged">
            <i:InvokeCommandAction Command="{Binding Path=SomeCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

But this does not work. It does however work when I use other events like the MouseEnter event.

Now my question is, why is that? What is the difference between the IsPressedChanged event and other events? It seems to me as if there is some conceptual difference and I'd like to know what that is (and maybe why there needs to be a difference).

Edit: I have found a way to have the command beeing executed on every IsPressed change. That solution is used in this question.

CodePudding user response:

The difference between IsPressedChanged and MouseEvent is that MouseEvent is an event provided by Button whereas IsPressedChanged is not.

Put differently, something that does not exist cannot really "work"...

CodePudding user response:

EventTrigger are used for routed event.
There is no IsPressedChanged routed event, the only routed event provided by a Button is Click and it doesn't match your needs.
In global routed events, you can find Mouse.MouseDown and Mouse.MouseUp but you will miss the cases where the button is pressed via the keyboard.

You can use the Button.IsPressed property via data trigger and by setting the binding source to be the button (by using ElementName or RelativeSource).
But you will have to bodge it a little so it trig either if the value is true or false.
Luckily, neither true or false are equal to the empty string. By setting DataTrigger.Comparison to "NotEqual" and Value to "" you ensure that the trigger is triggered each time IsPressed changed.
(A possible undesired effect: it trig as soon as the control is created.)

<Button x:Name="Button">
    <i:Interaction.Triggers>

<!--
        You can also use this binding
        <i:DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Button},  
                                         Path=IsPressed}"
-->

        <i:DataTrigger Binding="{Binding ElementName=Button, Path=IsPressed}"
                       Comparison="NotEqual"
                       Value="">
            <i:InvokeCommandAction Command="{Binding Path=PressedChangedCommand}"/>
        </i:DataTrigger>
    </i:Interaction.Triggers>
</Button>

Working demo available here.

  • Related