Bind Buttons to ScrollWheel and Keyboard - c#

In my application I have a Previous Button and a Next Button. When they are clicked, a particular function is called with different parameters.
I was wondering if there was a way that the Mouse ScrollWheel and the Left and Right keyboard keys could be bound to have the same result as clicking on these two Buttons?
I.E Pushing upwards on the ScrollWheel or Pressing the Right Key on the keyboard would be tantamount to clicking the Next Button and vice versa.

You can use Attached Behaviors. Create a behavior with command and parameter properties and bind. In the property changed just hook on needed events. Like here for MouseUp.
For the left and right key you could do it like this:
<Grid.InputBindings>
<KeyBinding Key="Left" Command="{Binding ExecutePreviousCommand}" />
<KeyBinding Key="Right" Command="{Binding ExecuteNextCommand}" />
</Grid.InputBindings>

Related

WPF handle shortcut inside TabControl (CTRL+F search)

I need to handle Ctrl+F keys combo to invoke search function inside my TabControl.
OnKeyDown() called for each button pressed, but how I can handle combination of two buttons?
Just put this in your xaml :
<Window.InputBindings>
<KeyBinding Command="ApplicationCommands.YourCommandToHandle"
Gesture="CTRL+F" />
</Window.InputBindings>

WPF Button Command for right mouse button?

I am learning about MVVM and Commands in WPF. I have a couple of buttons and I want to trigger similar commands depending on the fact if the buttons are clicked with the left or right mouse button.
Until now I used Event Handlers and I was able to determine which mouse button was pressed by checking the MouseButtonEventArgs.
<Button Content="Command Test" PreviewMouseDown="Button_PreviewMouseDown"/>
Current code behind:
private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
if (e.LeftButton == MouseButtonState.Pressed) {
Debug.Print("Left");
}
else {
Debug.Print("Right");
}
}
But I don’t see anything similar if I use Commands.
How can I set different commands for a button? One command when the left mouse button is clicked and another command when the right mouse button is clicked?
<Button Content="Command Test" Command="{Binding PressLetterCommand, Mode=OneWay}"/>
Currently the Command only fires when the left mouse button is clicked. If the command would also be fired if the right mouse button is clicked and I can find out which button was clicked that would be also a solution.
I searched and I found this question and answer which uses Decorator. How do I bind a command to e.g. the right mouse button in a ControlTemplate in WPF? I tried it but as far as I understand this won’t work for my buttons.
Any suggestions?
Try this
<Button Content="Command Test">
<Button.InputBindings>
<MouseBinding Gesture="RightClick" Command="{Binding PressLetterCommand}" />
</Button.InputBindings>
</Button>

WPF Bind button to key pressed

I have not done WPF development before.
I am creating a calculator. I want the "one" button to be bound to the keyboard key "1".
So the click function of my button must execute when the keyboard key "1" is pressed.
Please advise on how to do this>
use this:
http://msdn.microsoft.com/en-us/library/system.windows.input.keygesture.aspx
<Window.InputBindings>
<KeyBinding Command="{Binding MyCommand}"
Gesture="CTRL+R" />
</Window.InputBindings>

Silverlight: Simulate RadioButton Click event using UI Automation?

In Silverlight, both the Button and the RadioButton controls have a Click event, since they inherit from System.Windows.Controls.Primitives.ButtonBase.
If we want to simulate this Click event for a Button, we can use the ButtonAutomationPeer class, like so (given a button called myButton):
ButtonAutomationPeer peer = new ButtonAutomationPeer(myButton);
IInvokeProvider ip = (IInvokeProvider)peer;
ip.Invoke();
However, when we try to do the same thing for a RadioButton, we discover that the RadioButtonAutomationPeer class does not implement IInvokeProvider (so we can't call Invoke()). Is there some other way we can cause the Click event for a RadioButton to be fired?
Try this:
RadioButtonAutomationPeer peer = new RadioButtonAutomationPeer(myRadioButton);
IToggleProvider tp = (IToggleProvider) peer;
while (tp.ToggleState != targetToggleState)
{
tp.Toggle();
}
You'll need to know your desired toggle state (On, Off, or Indeterminate). Or if you just want to toggle to a different state, strike the while loop just call Toggle.
Obviously this isn't the exact same thing as a click. If you've bound the radio button to a command, you might be forced to be a little more explicit and do something like this to get your automation to work:
<RadioButton ... (don't set the Command property)>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding MyCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
Using the Blend SDK (that's the "i:" xmlns), you're invoking a command when the RadioButton is checked. Personally I'd do that approach even without automation over just binding a command to a RadioButton: it's not clear when the command executes with a binding, but the triggers add some clarity and remove the need for me to think the next time I look at the code.

Handling keyboard press in WPF

I am only beginning to learn MVVM and its use in WPF.
I am using it to build a very simple Calculator application (like the Windows built in calculator).
One thing i've noticed, is that binding my view (XAML) to commands does not let me configure WHEN these actual commands are fired.
For example, a Button control fires the command bound to it when it is clicked.
I would like to achieve the same effect when the numpad buttons are presed ('1' will fire the Command of the "1" Button control, and so on).
I could not find 1 way to do this, all sites seem to show multiple other options which don't seem valid in this case.
Should this be set up in the Control's (Button) level or on the entire window? How can i do this ?
Try to set input bindings of your main window.
<Window.InputBindings>
<KeyBinding Key="D1" Command="{Binding Command1}" />
<KeyBinding Key="NumPad1" Command="{Binding Command1}" />
</Window.InputBindings>

Categories

Resources