2012-01-17 8 views
4

を動作しない:オープンと保存のキーバインドが正常に動作コピー/貼り付けキーバインドは、私は私のメインウィンドウで、次のキーバインディングを持つ

<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"/> 

私はキーの組み合わせを打つとき...残りは何もしません。出力にはバインディングエラーはありません。私はまた、同じコマンドにバインドされたメニュー上のボタンを持っており、彼らは動作します。それらに関連付けられたCanExecuteメソッドを持つコマンドを使用する際に問題はありますか?私は.Net 4.0を使用しています。なぜクリップボードの動作がうまくいかないのかについてのアイデアはありますか?

更新: OpenCommandのような他のものをCtrl + Cにバインドすると機能します。私がCopyCommandを別のジェスチャにバインドすると、まだ動作しません。だから、このコマンドには問題があるようです。私のコピーボタンが同じCopyCommandに結び付けられているので、それは奇妙です。

+0

CopyCommandなどを別のジェスチャにバインドすると機能しますか? – Arcass

+0

Ctrl + Cに別のコマンドをバインドすると、CopyCommandを別のジェスチャーにバインドすると機能しません。それで、それはコマンドのものです。私はコマンドのコードを追加します。私が持っているコピーボタンがうまく働くので、奇妙に思える。 – KrisTrip

答えて

3

CanExecutetrueを返すコマンドを実行できるのは、実行しない理由の1つになる可能性があります。

別の考えられる理由は、デフォルトでTextBoxesというように、それぞれのジェスチャーのローカル処理です。独自のコマンドでローカルにKeyBindingsを再宣言することで、これを無効にすることができます。

+0

CanExecuteがtrueを返しています(私のボタンもそのコマンドにバインドされているため正しく使用できなくなります)。 – KrisTrip

+0

@KrisTrip:おそらくコマンドは他の場所で処理されるでしょうか?例えばTextBoxは前記「KeyBindings」をオーバーライドすることができる。 –

+0

ああ、私がコピーしようとしていることは、TreeView内のTextBox(読み取り専用)です。 TextBoxで無効にする方法はありますか? – KrisTrip

0

Windowsで既に使用されているため、使用できないキーの組み合わせがいくつかあります。 Ctrl + c/vはその1つだと思います。

+0

これは間違っています... –

1

これは問題なく動作します。私MainWindow.xamlファイルでは、私は私のMainWindow.xaml.csファイルでイラスト

<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> 

ための2つのキーバインドコマンドを追加し、私は次のように私のDataContextを初期化します。

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

MainWindowContextクラスは、私が実行すると

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; 
    } 
} 

に従うと定義され、それが正常に動作します。あなたはどのコマンドがあなたのコンソールで実行されたかを見ることができます。 私は何かが恋しいのか教えてください。