Keyboard shortcut for wpf menu item - c#

I am trying to add a keyboard shortcut to the menu item in my xaml code using
<MenuItem x:Name="Options" Header="_Options" InputGestureText="Ctrl+O" Click="Options_Click"/>
with Ctrl+O
But it is not working - it is not calling the Click option.
Are there any solutions for this?

InputGestureText is just a text. It does not bind key to MenuItem.
This property does not associate the input gesture with the menu item; it simply adds text to the menu item. The application must handle the user's input to carry out the action
What you can do is create RoutedUICommand in your window with assigned input gesture
public partial class MainWindow : Window
{
public static readonly RoutedCommand OptionsCommand = new RoutedUICommand("Options", "OptionsCommand", typeof(MainWindow), new InputGestureCollection(new InputGesture[]
{
new KeyGesture(Key.O, ModifierKeys.Control)
}));
//...
}
and then in XAML bind that command to some method set that command against MenuItem. In this case both InputGestureText and Header will be pulled from RoutedUICommand so you don't need to set that against MenuItem
<Window.CommandBindings>
<CommandBinding Command="{x:Static local:MainWindow.OptionsCommand}" Executed="Options_Click"/>
</Window.CommandBindings>
<Menu>
<!-- -->
<MenuItem Command="{x:Static local:MainWindow.OptionsCommand}"/>
</Menu>

you should succeed in this way:
Defining MenuItem Shortcuts
By using KeyBindings:
<Window.CommandBindings> <CommandBinding Command="New" Executed="CommandBinding_Executed" /> </Window.CommandBindings> <Window.InputBindings> <KeyBinding Key="N" Modifiers="Control" Command="New"/> </Window.InputBindings>

Related

How to add to Click event to Context Menu buttons in WPF NotifyIcon?

I am making an app that has a notify icon in WPF. I am using HardCodet NotifyIcon. They do have a tutorial on code project and it is pretty useful but it does not have any explanation on how to set up OnClick or Click event when the buttons in the context menu are pressed.
I have gone through every property in NotifyIcon.ContextMenu.Items and NotifyIcon.ContextMenu.Items.GetItemAt(i) (TaskbarIcon NotifyIcon = (TaskbarIcon) FindResource("MyNotifyIcon")) but there is nothing I found. I also tried typecasting the buttons to MenuItem and using its Click event but it didn't help.
This is my App.xaml:
<Application.Resources>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:tb="http://www.hardcodet.net/taskbar">
<tb:TaskbarIcon x:Key="MyNotifyIcon"
ToolTipText="Hello There">
<tb:TaskbarIcon.ContextMenu>
<ContextMenu Background="White">
<MenuItem Header="Open"/>
<MenuItem Header="Settings"/>
<MenuItem Header="Sign Out"/>
<MenuItem Header="Help"/>
<MenuItem Header="Exit"/>
</ContextMenu>
</tb:TaskbarIcon.ContextMenu>
</tb:TaskbarIcon>
</ResourceDictionary>
</Application.Resources>
I need the buttons to control the MainWindow e.g. change the Visibility etc..
There is no difference to other controls, you can just set up a Click handler on each MenuItem.
<MenuItem Header="Open" Click="Open_OnClick"/>
In your example, you would implement the event handler in App.xaml.cs.
public partial class App : Application
{
// ...application code.
private void Open_OnClick(object sender, RoutedEventArgs e)
{
// ...do something.
}
}
You could also assign a view model as DataContext to TaskbarIcon and use a command instead.

Fire a command from a Button inside a ContentControl?

I'm new to WPF and I'm trying to dynamically add a Button inside a ContentControl, which should fire a command when clicked. I'm using MVVMLight to handle the Commands.
Below I have an example with two buttons. The top button is placed directly into the StackPanel. This button fires off the Command as expected.
The second button is placed inside a ContentControl. It displays correctly, but the Command does not fire when the button is clicked.
I assumed this is because the Binding does not transfer down through the DataTemplate, but it seems to work if I use regular Commands instead of MVVMLight RelayCommands.
I don't want to remove the framework, so I'm wondering if anyone knows how to fix it? Thanks
<Window x:Class="ContentControlExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:ContentControlExample.ViewModel">
<Window.DataContext>
<vm:MainViewModel />
</Window.DataContext>
<Window.Resources>
<DataTemplate x:Key="MyButton" >
<Button Content="SUBMIT" Command="{Binding MyCommand}" Width="200" Height="50"/>
</DataTemplate>
</Window.Resources>
<StackPanel>
<!--When this button is clicked, the Command executes as expected-->
<Button Content="SUBMIT" Command="{Binding MyCommand}" Width="200" Height="50"/>
<!--Nothing happens when this button is clicked-->
<ContentControl ContentTemplate="{StaticResource MyButton}"/>
</StackPanel>
</Window>
Here's the ViewModel with the command:
public class MainViewModel : ViewModelBase
{
public ICommand MyCommand { get; private set; }
public MainViewModel()
{
MyCommand = new RelayCommand(MyCommand_Executed, MyCommand_CanExecute);
}
private bool MyCommand_CanExecute()
{
return true;
}
private void MyCommand_Executed()
{
MessageBox.Show("The command executed");
}
}
The problem here is the implicit DataContext in ContentTemplate is the Content and this has not been set to anything. You need to set Content to some Binding to bridge the DataContext currently in the visual tree, something like this:
<ContentControl ContentTemplate="{StaticResource MyButton}" Content="{Binding}"/>
Another solution is to give your Window a name:
<Window x:Class="ContentControlExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:ContentControlExample.ViewModel"
x:Name="_this">
Then bind via its context instead:
<Button Content="SUBMIT" Command="{Binding ElementName=_this, Path=DataContext.MyCommand}" Width="200" Height="50"/>
This is particularly handy for things like ListViews and ItemControls, as their DCs get set to the list elements. Keep in mind though that this will only work on members within the same visual tree, if that's not the case (e.g. popup menus etc) then you need to proxy a binding as described in this article.

Set KeyBinding on a whole WPF window

I working on an MVVM window and want to control something in the view model by the keyboard, but if i place the following code directly under the window it can't be compiled only if i place under for example a text box. How can i do this?
<KeyBinding Key="P" Command="{Binding ToggleCommand}"/>
You need to assign the KeyBinding to the InputBindings property on Window
<Window.InputBindings>
<KeyBinding Key="P" Command="{Binding ToggleCommand}"/>
</Window.InputBindings>

CommandBinding relative path

Within my window (let's say MyCanvas) there's a new command definition which I then bind to my window. Usually the handlers for CanExecute and Executed are directly within MyCanvas. But is it somehow possible to bind the CanExecute and Executed handlers to a relative path like for example MyCanvas.Logic.CanExecute_OpenCamera?
This is my current XAML.
<Window.Resources>
<RoutedUICommand x:Key="OpenCamera" Text="Open camera" />
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="{StaticResource OpenCamera}" CanExecute="CanExecute_OpenCamera" Executed="Executed_OpenCamera"/>
</Window.CommandBindings>
This is what I'm trying to define. Logic is public property of MyCanvas.
<Window.Resources>
<RoutedUICommand x:Key="OpenCamera" Text="Open camera" />
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="{StaticResource OpenCamera}" CanExecute="Logic.CanExecute_OpenCamera" Executed="Logic.Executed_OpenCamera"/>
</Window.CommandBindings>
You can, but you have to use converters which create delegates to the respective methods from a bound object of the class, or from its type, if the methods are static. See http://wpfglue.wordpress.com/2012/05/07/commanding-binding-controls-to-methods/ for details.

how to add multiple command bindings in wpf

I am working with WPF. I want to create keyboard shortcuts for my WPF application. I have created as following. The first command binding tag for "open" is working and command binding for exit is not working. I dont know what is the reason.
<Window.CommandBindings>
<CommandBinding Command="Open" Executed="CommandBinding_Executed"/>
<CommandBinding Command="Exit" Executed="CommandBinding_Executed_1" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Command="Open" Key="O" Modifiers="control" />
<KeyBinding Command="Exit" Key="E" Modifiers="control"/>
</Window.InputBindings>
Above code is getting the following error:
Cannot convert string 'Exit' in attribute 'Command' to object of type
'System.Windows.Input.ICommand'. CommandConverter cannot convert from
System.String. Error at object 'System.Windows.Input.CommandBinding' in
markup file 'WpfApplication2;component/window1.xaml' Line 80 Position 25.
You problem is that there is no exit command. You'll have to roll your own.
See here for built-in ApplicationCommands
It's pretty easy to create your own, I use a static utility class to hold common commands that I use often. Something like this:
public static class AppCommands
{
private static RoutedUICommand exitCommand = new RoutedUICommand("Exit","Exit", typeof(AppCommands));
public static RoutedCommand ExitCommand
{
get { return exitCommand; }
}
static AppCommands()
{
CommandBinding exitBinding = new CommandBinding(exitCommand);
CommandManager.RegisterClassCommandBinding(typeof(AppCommands), exitBinding);
}
}
Then you should be able to bind it like this:
<KeyBinding Command="{x:Static local:AppCommands.Exit}" Key="E" Modifiers="control"/>

Categories

Resources