Binding to other controlls InputBindings - c#

I would like to Bind Keybindings of my Button to the Keybindings of my Window.
My Window (Name="Mainwindow) Input Binding:
<Window.InputBindings>
<KeyBinding Key="Return" Command="{Binding KeyPressed}" CommandParameter="Next"/>
</Window.InputBindings>
My idea of what the Button should look like
<Button Content="Select Directory" Command="{Binding BrowseDirectory}">
<Button.InputBindings>
<KeyBinding Key="Return" Command="{Binding ElementName=Mainwindow, Path=InputBindings}"/>
</Button.InputBindings>
</Button>
Sadly the Outwindow did not display any kind of Binderror any anything else to give me a clue on why it is not working.
So baisically the question is: How can I Bind the Control.InputBindings to another Control.InputBindings?

Why don't you just bind the Command property to the same source property:
<Button Content="Select Directory" Command="{Binding BrowseDirectory}">
<Button.InputBindings>
<KeyBinding Key="Return" Command="{Binding Path=DataContext.KeyPressed, RelativeSource={RelativeSource AncestorType=Window}}"/>
</Button.InputBindings>
</Button>

Related

WPF Command not executed when viewmodel changes

I'm new to WPF and I don't know why binding from the viewmodel does not execute the specified command.
<CheckBox x:Name="CheckBoxName" Content="MyCheckBox" IsChecked="{Binding Restrictions.MyCheckBox}" Command="{Binding MyCommand}" CommandParameter="{Binding ElementName=CheckBoxName}"/>
When I check the checkbox in the view, MyCommand is executed as expected, but when I set the IsChecked property from the viewmodel using binding, the command is not executed.
Does anyone have any idea why it's happening? How can I execute the command from viewmodel using binding?
Thanks you very much!
As far as I know, the Command is executed on the "Click" event of the base "ButtonBase" class, which is not triggered if you change the IsChecked Property via Binding.
One solution would be to attach a trigger to the Checked and Unchecked event, which triggers your command.
First you need to add the namespace
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
And for your checkbox
<CheckBox x:Name="CheckBoxName"
Content="MyCheckBox"
IsChecked="{Binding Restrictions.MyCheckBox}"
Command="{Binding MyCommand}"
CommandParameter="{Binding ElementName=CheckBoxName}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding MyCommand}" CommandParameter="{Binding Path=IsChecked, ElementName=CheckBoxName}" />
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding MyCommand}" CommandParameter="{Binding Path=IsChecked, ElementName=CheckBoxName}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Checkbox>

WPF Input Key Binding

<Window.InputBindings>
<KeyBinding Key="0" Command="{Binding ZeroCommand}"/>
<KeyBinding Key="1" Command="{Binding OneCommand}"/>
<KeyBinding Key="2" Command="{Binding TwoCommand}"/>
<KeyBinding Key="3" Command="{Binding ThreeCommand}"/>
<KeyBinding Key="4" Command="{Binding FourCommand}"/>
<KeyBinding Key="5" Command="{Binding FiveCommand}"/>
<KeyBinding Key="6" Command="{Binding SixCommand}"/>
<KeyBinding Key="7" Command="{Binding SevenCommand}"/>
<KeyBinding Key="8" Command="{Binding EightCommand}"/>
<KeyBinding Key="9" Command="{Binding NienCommand}"/>
<KeyBinding Key="OemPlus" Command="{Binding PlusCommand}"/>
<KeyBinding Key="OemMinus" Command="{Binding MinusCommand}"/>
<KeyBinding Key="OemBackslash" Command="{Binding DevideCommand}"/>
<KeyBinding Key="Multiply" Command="{Binding MultiplyCommand}"/>
<KeyBinding Key="Return" Command="{Binding EqualsCommand}"/>
</Window.InputBindings>
I was creating a basic calculator app when I ran into this issue. I am getting a compilation error '0' can not be used as a value for 'Key'. Numbers are not valid enumeration values. In fact OemMinus is the only command that is being bound.
I think could fix this by creating a InputBindingConfiguration class with a singleton, define the keys there and bind to those keys but I was wondering if there was a way of doing this purely in Xaml ?
check the proper key binding below
Link : WPFKeyBindings
Basic Keys
Key - Code Key(s)
A - Z The basic letter keys.
D0 - D9 The numeric keys from the main section of the keyboard.
Space The space bar.
F1 - F24 The function keys.

Is there actually a way to bind Checked event of a radio button to a command in the ViewModel?

I tried with
<RadioButton Content="Boom" Command={Binding MyCommand} IsEnabled="{Binding IsChecked, Converter={StaticResource InverseBooleanConverter}, RelativeSource={RelativeSource Mode=Self}}"/>
but nothing happens. Why is that and how to fix it?
Step 1: Add System.Windows.Interactivity reference
Step 2: Add Namespace in XAML xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Step 3:
<RadioButton Content="Boom" IsEnabled="{Binding IsChecked, Converter={StaticResource InverseBooleanConverter}, RelativeSource={RelativeSource Mode=Self}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding MyCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
The following code works as expected:
<RadioButton Content="Boom" Command="{Binding MyCommand}" />
That is, as in case of a regular Button, MyCommand is fired every time you click a RadioButton. If you're using RadioButtons, this, understandably, may not be what you want.
More useful will be passing some sort of data as a CommandParameter to know which option was checked:
<RadioButton Content="AAA" Command="{Binding MyCommand}" CommandParameter="AAA" GroupName="MyGroup"/>
<RadioButton Content="BBB" Command="{Binding MyCommand}" CommandParameter="BBB" GroupName="MyGroup"/>
Example command method:
private ICommand _MyCommand;
public ICommand MyCommand
{
get { return _MyCommand ?? (_MyCommand = new DelegateCommand(a => MyCommandMethod(a))); }
}
private void MyCommandMethod(object item)
{
Console.WriteLine("Chosen element: " + (string)item);
}

Implementing an Edit menu in MVVM Light application

I'm building a WPF application using MVVM Light. I've added a menu to my window that includes the standard File and Edit menus:
<Window x:Class="ParserEditor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ParserEditor"
xmlns:ignore="http://www.galasoft.ch/ignore"
xmlns:i="clr namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
mc:Ignorable="d ignore"
Closing="Window_Closing"
DataContext="{Binding Main, Source={StaticResource Locator}}"
Height="{Binding Height, Mode=TwoWay}"
MaxHeight="{Binding MaxHeight, Mode=TwoWay}"
MinHeight="{Binding MinHeight, Mode=TwoWay}"
MinWidth="450"
Width="450"
Title="{Binding WindowTitle}">
<Window.InputBindings>
<KeyBinding Command="{Binding NewParserCommand}" Gesture="CTRL+N" />
<KeyBinding Command="{Binding OpenParserCommand}" Gesture="CTRL+O" />
<KeyBinding Command="{Binding SaveParserCommand}" Gesture="CTRL+S" />
<KeyBinding Command="{Binding SaveParserAsCommand}" Gesture="ALT+A" />
<KeyBinding Command="{Binding ExitCommand}" Gesture="ALT+F4" />
<KeyBinding Command="{Binding ApplicationCommands.Undo}" Gesture="CTRL+Z" />
<KeyBinding Command="{Binding ApplicationCommands.Redo}" Gesture="CTRL+Y" />
<KeyBinding Command="{Binding ApplicationCommands.Cut}" Gesture="CTRL+X" />
<KeyBinding Command="{Binding ApplicationCommands.Copy}" Gesture="CTRL+C" />
<KeyBinding Command="{Binding ApplicationCommands.Paste}" Gesture="CTRL+V" />
<KeyBinding Command="{Binding ApplicationCommands.Delete}" Gesture="DEL" />
<KeyBinding Command="{Binding ApplicationCommands.SelectAll}" Gesture="CTRL+A" />
</Window.InputBindings>
<DockPanel Name="Dock">
<Menu IsMainMenu="true" DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_New..." InputGestureText="Ctrl-N" Command="{Binding NewParserCommand}" />
<MenuItem Header="_Open..." InputGestureText="Ctrl-O" Command="{Binding OpenParserCommand}" />
<MenuItem Header="_Save..." InputGestureText="Ctrl-S" Command="{Binding SaveParserCommand}" />
<MenuItem Header="Save _As..." InputGestureText="Alt-A" Command="{Binding SaveParserAsCommand}" />
<Separator />
<MenuItem Header="E_xit" InputGestureText="Alt-F4" Command="{Binding ExitCommand}" />
</MenuItem>
<MenuItem Header="_Edit">
<MenuItem Header="Undo" InputGestureText="Ctrl-Z" />
<MenuItem Header="Redo" InputGestureText="Ctrl-Y" />
<Separator/>
<MenuItem Header="Cut" InputGestureText="Ctrl-X" />
<MenuItem Header="Copy" InputGestureText="Ctrl-C" />
<MenuItem Header="Paste" InputGestureText="Ctrl-V" />
<MenuItem Header="Delete" InputGestureText="Del" />
<MenuItem Header="Select All" InputGestureText="Ctrl-A" />
</MenuItem>
</Menu>
<Grid x:Name="LayoutRoot">
<!-- Some controls, including TextBlocks & TextBoxes here -->
</Grid>
</DockPanel>
</Window>
Now, if I run the application and make some edits in one of the TextBox controls, I can use the usual keyboard short cuts to cut, copy & paste text, undo & redo actions while in focus is the control. But if I click on my Edit menu and use one of my options there, nothing happens.
How do I get my menu items to work?
I figured this out. After posting the question & reviewing the XAML, I noticed that the Edit menu items didn't have any Commands bound to them. I added the required Command="ApplicationCommands.xxx" statements to the XAML & now the Edit menu works.

KeyBinding for gestures CTRL+C and CTRL+ V do not work?

I have a few ICommands in my View Model which I would like to bind them using Key Board bindings to my user control. The problem I am facing is that they are not fired when I use CTRL + C and CTRL + V to bind my copy and paste commands in my UserControl. Am I supposed to override them or something?.
<UserControl.InputBindings>
<KeyBinding Gesture="CTRL+C" Command="{Binding CopyCommand}" />
</UserControl.InputBindings>
This works for me like a charm:
<!--COPY-->
<UserControl.InputBindings>
<KeyBinding Key="C" Modifiers="Ctrl" Command="{Binding CopyToStackCommand}" />
</UserControl.InputBindings>
<!--PASTE-->
<UserControl.InputBindings>
<KeyBinding Key="V" Modifiers="Ctrl" Command="{Binding PasteFromStackCommand}" />
</UserControl.InputBindings>
<KeyBinding Key="C" Modifiers="Ctrl" Command="Copy" CommandParameter="{Binding copy}" />

Categories

Resources