1

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.

Felix
  • 1,066
  • 1
  • 5
  • 22
  • I think you want to use `OnIsPressedChanged` instead `IsPressedChanged` – ɐsɹǝʌ ǝɔıʌ May 13 '22 at 12:12
  • There is no such event named "IsPressedChanged". There is a dependency property "IsPressed" whose changes you could observe / react on. – lidqy May 13 '22 at 12:14
  • @Felix: Can't you handle the `MouseLeftButtonDown` and `MouseLeftButtonUp` events? – mm8 May 13 '22 at 13:06
  • @mm8: Yes, but these would not work with pointing devices other than a mouse. – Felix May 13 '22 at 13:08
  • 1
    @mm8: Thats correct. My question was WHY it did not work and AFigmentOfMyImagination did perfectly answer that. (I have already figured out how to hadle the event using a custom button implementation and a routed CustomIsPressedChanged event. Which i will post another question on concearning another propblem once I am allowed to.) – Felix May 13 '22 at 13:16

2 Answers2

2

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"...

2

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.

Orace
  • 7,822
  • 30
  • 45
  • You can remove the `Value=""` line since `unset` is neither equals to `True` or `False` too. But you may loose in clarity. – Orace May 13 '22 at 14:02
  • Wow, this is a great solution. This is way more compact than creating a custom button which fires a new routed event from the OnIsPressedChanged method. Thank you very much! – Felix May 13 '22 at 14:20