Roest
Topic Author
Posts: 35
Joined: 15 Jan 2020, 15:30

Button command on right click

17 May 2020, 17:18

Is there a way to execute a command on right clicking a button?

Something like
<Button.InputBindings>
    <MouseBinding Gesture="RightClick" 
        Command="{Binding command }" 
        CommandParameter="{Binding param}"/>
</Button.InputBindings>
doesn't work because MouseBinding doesn't seem to exist in Noesis. So what's the recommended way?
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Button command on right click

22 May 2020, 16:16

You can use an interactivity trigger reacting to MouseRightButtonDown:
<Button>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseRightButtonDown">
      <i:InvokeCommandAction Command="{Binding command}" CommandParameter="{Binding param}"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Button>
 
HateDread
Posts: 74
Joined: 06 Feb 2020, 10:27

Re: Button command on right click

31 Oct 2021, 08:35

You can use an interactivity trigger reacting to MouseRightButtonDown:
<Button>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseRightButtonDown">
      <i:InvokeCommandAction Command="{Binding command}" CommandParameter="{Binding param}"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Button>
Found this thread looking for a solution, here was my initial xaml that worked only for left click:
[code]
<Button DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" Content="" Background="#00DDDDDD" Grid.ColumnSpan="2" Margin="-5,-5,-5,-5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="#FF707070" Panel.ZIndex="1" Command="{Binding RouteOnClicked}" CommandParameter="{Binding DataContext}"/>
Then I needed double click support, so had to switch to this, which works in Blend / without Noesis, but doesn't work in Noesis:
<Button DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" Content="" Background="#00DDDDDD" Grid.ColumnSpan="2" Margin="-5,-5,-5,-5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="#FF707070" Panel.ZIndex="1">
	<Button.InputBindings>
		<MouseBinding Gesture="LeftClick" Command="{Binding RouteOnClicked}" CommandParameter="{Binding DataContext}"></MouseBinding>
		<MouseBinding Gesture="LeftDoubleClick" Command="{Binding RouteOnDoubleClicked}" CommandParameter="{Binding DataContext}"></MouseBinding>
	</Button.InputBindings>
</Button>
I tried to update it to use your suggestion (simplifying to just single left click for now):
<Button DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" Content="" Background="#00DDDDDD" Grid.ColumnSpan="2" Margin="-5,-5,-5,-5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="#FF707070" Panel.ZIndex="1">
	<i:Interaction.Triggers>
		<i:EventTrigger EventName="MouseLeftButtonDown">
			<i:InvokeCommandAction Command="{Binding RouteOnClicked}" CommandParameter="{Binding DataContext}"/>
		</i:EventTrigger>
	</i:Interaction.Triggers>
</Button>
But now it doesn't work at all. This is the view in C++:
class ShipListEntryView : public Noesis::UserControl
{
public:
	ShipListEntryView()
	{
		InitializeComponent();
	}

	void InitializeComponent()
	{
		Noesis::GUI::LoadComponent(this, "Controls/ShipListEntryView.xaml");
	}

	bool ConnectEvent(BaseComponent* source, const char* event, const char* handler) override
	{
		NS_CONNECT_EVENT(Noesis::Button, Click, OnClicked);
		return false;
	}

	void OnClicked(Noesis::BaseComponent* sender, const Noesis::RoutedEventArgs& e)
	{
		Noesis::Ptr<NoesisApp::DelegateCommand> onSelectedProperty = GetValue<Noesis::Ptr<NoesisApp::DelegateCommand>>(OnSelectedProperty_);
		onSelectedProperty->Execute(sender);
	}

private:
	inline static const Noesis::DependencyProperty* OnSelectedProperty_;
	inline static const Noesis::DependencyProperty* OnFocusedProperty_;

	NS_IMPLEMENT_INLINE_REFLECTION(ShipListEntryView, UserControl, "rtg.Controls.ShipListEntryView")
	{
		Noesis::DependencyData* data = NsMeta<Noesis::DependencyData>(Noesis::TypeOf<ShipListEntryView>());
		data->RegisterProperty<Noesis::Ptr<NoesisApp::DelegateCommand>>(OnSelectedProperty_, "RouteOnClicked",
			Noesis::PropertyMetadata::Create(Noesis::DependencyProperty::GetUnsetValue()));
	}
};
Some questions;

1. Why can't I use MouseBinding?
2. How should I make my xaml work in both Blend and Noesis runtime / in-game? Blend doesn't like the triggers and I need that to have a workflow.
3. Where can I find a list of trigger names? E.g. "MouseLeftButtonDown". Not sure what to use for double-clicking.
4. What am I missing in the above code?
 
HateDread
Posts: 74
Joined: 06 Feb 2020, 10:27

Re: Button command on right click

31 Oct 2021, 08:43

I found this ticket for MouseBinding... from 2017: https://www.noesisengine.com/bugs/view.php?id=1161 😱
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Button command on right click

01 Nov 2021, 13:11

Hi,

There were no many request about supporting MouseBindings because you can achieve the same using interaction triggers:
<Button Content="..." Command="{Binding LeftClick}" CommandParameter="{Binding}">
    <b:Interaction.Triggers>
      <b:EventTrigger EventName="MouseDoubleClick">
        <b:InvokeCommandAction Command="{Binding DoubleClick}" CommandParameter="{Binding}"/>
      </b:EventTrigger>
    </b:Interaction.Triggers>
</Button>
To use the interaction triggers in Blend you should add our extensions nuget to your Blend project: https://www.nuget.org/packages/Noesis.GUI.Extensions

MouseLeftButtonDown event is handled by the Button control, so you cannot listen to that event directly, you should use the Preview instead. But if you are going to just invoke a command when the user clicks with the mouse left button, it is better to just set the Command property of the button that will be executed when the button raises the Click event.

UIElement provides most of the input events, but the double-click is generated by the Control class. In our documentation you can see the list of events provided by each class.

Hope this help.

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests