Copy / paste

My MainWindow has the following key bindings:

<KeyBinding Command="{Binding OpenCommand}" Gesture="Ctrl+O"/> <KeyBinding Command="{Binding SaveCommand}" Gesture="Ctrl+S"/> <KeyBinding Command="{Binding CopyCommand}" Gesture="Ctrl+C"/> <KeyBinding Command="{Binding PasteCommand}" Gesture="Ctrl+V"/> <KeyBinding Command="{Binding CutCommand}" Gesture="Ctrl+X"/> 

The Open and Save key commands work fine ... the rest do nothing when I press a key combination. There are no binding errors at the output. I also have buttons in my menu associated with the same commands, and they work. Is there a problem with using commands that have the CanExecute method associated with them? I am using .Net 4.0. Any ideas on why clipboard actions won't work?

Update: If I bind something else (e.g. OpenCommand) to Ctrl + C, this will work. If I associate CopyCommand with another gesture, it still won't work. So this is a problem with the team. This is strange because my copy button works fine with the same CopyCommand. Here is the CopyCommand code to which it is bound:

 public ICommand CopyCommand { get { if (this.copyCommand == null) { this.copyCommand = new RelayCommand( param => this.Copy(), param => this.Copy_CanExecute()); } return this.copyCommand; } } 
+4
source share
3 answers

You can execute commands where CanExecute returns true , may be one of the reasons they are not executed.

Another possible reason is the local processing of the corresponding gestures, since TextBoxes do by default. You can override this by re-declaring KeyBindings locally with your own command.

+3
source

this works great. In my MainWindow.xaml file, I add two keyBinding commands to illustrate

 <Window x:Class="MainWindowCommandBinding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.InputBindings> <KeyBinding Command="{Binding OpenCommand}" Gesture="Ctrl+O"/> <!--<KeyBinding Command="{Binding SaveCommand}" Gesture="Ctrl+S"/>--> <KeyBinding Command="{Binding CopyCommand}" Gesture="Ctrl+C"/> <!--<KeyBinding Command="{Binding PasteCommand}" Gesture="Ctrl+V"/> <KeyBinding Command="{Binding CutCommand}" Gesture="Ctrl+X"/>--> </Window.InputBindings> <Grid> </Grid> </Window> 

In my MainWindow.xaml.cs file, I initialize my DataContext as follows.

  public MainWindow() { InitializeComponent(); DataContext = new MainWindowContext(); } 

The MainWindowContext class is defined as follows:

 class MainWindowContext { RelayCommand _openCommand; public ICommand OpenCommand { get { if (_openCommand == null) { _openCommand = new RelayCommand( param => this.Open(), param => this.Open_CanExecute()); } return _openCommand; } set { _openCommand = (RelayCommand) value; } } RelayCommand _copyCommand; public ICommand CopyCommand { get { if (_copyCommand == null) { _copyCommand = new RelayCommand( param => this.Copy(), param => this.Copy_CanExecute()); } return _copyCommand; } set { _copyCommand = (RelayCommand)value; } } private bool Copy_CanExecute() { return true; } private object Copy() { Console.Out.WriteLine("Copy command executed"); return null; } private bool Open_CanExecute() { return true; } private object Open() { Console.Out.WriteLine("Open command executed"); return null; } } 

When I execute, it works fine. You can see which command was executed in the console. Please tell me if I miss something.

+1
source

There are several key combinations that you cannot use, since windows already use them. I think Ctrl + c / v is one of them.

0
source

Source: https://habr.com/ru/post/1391463/


All Articles