Page 1 of 1

Handling UIElement events in UE5

Posted: 19 Dec 2023, 15:39
by DominikRuta
Hello, I have a simple question. Is there a way how to listen on UIElement events and handle them in UE5 c++ class while still getting event arguments like this?
	void UMyUnrealView::OnMouseWheel(BaseComponent* component, const Noesis::MouseWheelEventArgs& e)
	{
		// Do some stuff
		return;
	}
Registering the callback in XAML would look something like this:
	<Canvas MouseWheel="OnMouseWheel">
		...
	</Canvas>
I know how to listen on them like this using Behaviors, but in some cases, I need context of that view object.

Thank you

Re: Handling UIElement events in UE5

Posted: 19 Dec 2023, 18:12
by hcpizzi
If I understand it correctly, you want to be able to receive the event arguments in a handler function in your ViewModel, which is an UObject derived class. Am I right?

In that case, that is not possible at the moment. If all you need is to know the source of the event, that could be done using behaviors.

Re: Handling UIElement events in UE5

Posted: 22 Dec 2023, 10:20
by sfernandez
It's already been reported the possibility to receive event arguments as the command parameter in the InvokeCommandAction, but it's still not implemented: #2723

If you only need to receive the element that sent the event (although it is rare that in a ViewModel you need to know about a specific UI element of the view), you can pass it as the command parameter:
<Canvas x:Name="canvas" Background="Transparent">
  <b:Interaction.Triggers>
    <b:EventTrigger EventName="MouseWheel">
      <b:InvokeCommandAction Command="{Binding SomeCommand}" CommandParameter="{Binding ElementName=canvas}"/>
    </b:EventTrigger EventName="MouseWheel">
  </b:Interaction.Triggers>
</Canvas>