Button command on right click
Is there a way to execute a command on right clicking a button?
Something like
doesn't work because MouseBinding doesn't seem to exist in Noesis. So what's the recommended way?
Something like
Code: Select all
<Button.InputBindings>
<MouseBinding Gesture="RightClick"
Command="{Binding command }"
CommandParameter="{Binding param}"/>
</Button.InputBindings>
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: Button command on right click
You can use an interactivity trigger reacting to MouseRightButtonDown:
Code: Select all
<Button>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseRightButtonDown">
<i:InvokeCommandAction Command="{Binding command}" CommandParameter="{Binding param}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>Re: Button command on right click
Found this thread looking for a solution, here was my initial xaml that worked only for left click:You can use an interactivity trigger reacting to MouseRightButtonDown:Code: Select all<Button> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseRightButtonDown"> <i:InvokeCommandAction Command="{Binding command}" CommandParameter="{Binding param}"/> </i:EventTrigger> </i:Interaction.Triggers> </Button>
Code: Select all
[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}"/>
Code: Select all
<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>
Code: Select all
<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>
Code: Select all
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()));
}
};
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?
Re: Button command on right click
I found this ticket for MouseBinding... from 2017: https://www.noesisengine.com/bugs/view.php?id=1161 😱
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: Button command on right click
Hi,
There were no many request about supporting MouseBindings because you can achieve the same using interaction triggers:
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.
There were no many request about supporting MouseBindings because you can achieve the same using interaction triggers:
Code: Select all
<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>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: GamingPo and 4 guests