2017-07-14 17 views
0

ウィンドウレベルのKeyBindingをコマンドパラメータでウィンドウ自体として使用したいと考えています。入力バインディングCommandParameterウィンドウにバインド

<KeyBinding Command="{Binding CloseCommand}" CommandParameter="{Binding ElementName=mainWindow}" Key="Esc"/> 

バインディングは機能しますが、パラメータはnullになります。何が回避策ですか?続き

は私のコマンドです: `

public class DelegateCommand : ICommand 
{ 
    private readonly Predicate<object> _canExecute; 
    private readonly Action<object> _execute; 

    public DelegateCommand(Action<object> execute) 
     : this(execute, null) 
    { 
    } 

    public DelegateCommand(Action<object> execute, 
        Predicate<object> canExecute) 
    { 
     if (execute == null) 
      throw new ArgumentNullException("Action excute is null"); 
     _execute = execute; 
     _canExecute = canExecute; 
    } 

    [DebuggerStepThrough] 
    public bool CanExecute(object parameter) 
    { 
     return _canExecute == null ? true : _canExecute(parameter); 
    } 

    public void Execute(object parameter) 
    { 
     _execute(parameter); 
    } 

    public event EventHandler CanExecuteChanged 
    { 
     add { CommandManager.RequerySuggested += value; } 
     remove { CommandManager.RequerySuggested -= value; } 
    } 
+0

あなたはどのような 'コマンド 'を使用していますか? – Peter

+0

あなたの 'Window'には名前があり、あなたのコマンドはどのように見えますか?私はスヌーピーだから、なぜあなたはパラメータとしてウィンドウを使いたいのですか?あなたは何をしたいですか? –

答えて

1

実際には私のために働いていますが、[MVVM Light Toolkit 1]のRelayCommand<FrameworkElement>を使用しています。私の場合は

<Window.InputBindings> 
    <KeyBinding Command="{Binding MyCommand, ElementName=MainRoot}" CommandParameter="{Binding ElementName=MainRoot}" Key="Esc"/> 
</Window.InputBindings> 

CommandDependencyPropertyから来ているが、それは大きな違いを生むべきではありません。

だから、
public RelayCommand<FrameworkElement> MyCommand 
{ 
    get { return (RelayCommand<FrameworkElement>)GetValue(MyCommandProperty); } 
    set { SetValue(MyCommandProperty, value); } 
} 

// Using a DependencyProperty as the backing store for MyCommand. This enables animation, styling, binding, etc... 
public static readonly DependencyProperty MyCommandProperty = 
    DependencyProperty.Register("MyCommand", typeof(RelayCommand<FrameworkElement>), typeof(MainWindow), new PropertyMetadata(null)); 




public MainWindow() 
{ 
    InitializeComponent(); 
    MyCommand = new RelayCommand<FrameworkElement>(DoSthYo); 
} 

public void DoSthYo(FrameworkElement fwE) 
{ 
    var x = fwE; 
} 

これが働いているので - 私は多分CommandParameterをサポートしていないそのあなたのCommandを考えます。

+0

コマンドを含むようにmy Qを編集しました。実装を試してみるために、私はMVVM Toolkitを使用していません。 –

+0

おそらくhttps://github.com/paulcbetts/mvvmlight/blob/dce4e748c538ed4e5f5a0ebbfee0f54856f52dc6/V3/GalaSoft.MvvmLight/GalaSoft.MvvmLight%20(NET35)/Command/RelayCommandGeneric.csにヒントがあります。 – Peter

+0

これは面白いです、例えばボタン上の私のコマンドactrantは、CommandParameterとしての要素バインディングを取ります。仕事をしても入力バインディングは受けません。

0

私のアドバイスは結合あなたCommandParameterRelativeSourceを使用することです:あなたの結合は、あなたのWindowの名前から独立したことができます。このように

<Window.InputBindings> 
    <KeyBinding Command="{x:Static local:CloseWindowCommand.Instance}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" Key="Esc" /> 
</Window.InputBindings> 

を。 は、次に、あなたのアプリケーションの各ウィンドウを閉じるためのstaticコマンドを作成することができます。

public class CloseWindowCommand : ICommand 
{ 
    public static readonly ICommand instance = new CloseWindowCommand(); 
    public event EventHandler CanExecuteChanged; 

    private CloseWindowCommand() 
    { 
    } 

    public bool CanExecute(object parameter) 
    { 
     return (parameter is Window); 
    } 

    public void Execute(object parameter) 
    { 
     Window win; 
     if (CanExecute(parameter)) 
     { 
      win = (Window)parameter; 
      win.Close(); 
     } 
    } 
} 

私はそれはあなたを助けることができると思います。

関連する問題