2017-07-14 86 views
0

我想要一個窗口級別的KeyBinding命令參數作爲窗口本身。輸入綁定CommandParameter綁定到窗口

例如

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

綁定工作,但參數爲空。什麼是工作?

以下是我的命令:`

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

你正在使用什麼樣的'Command'? – Peter

+0

你的'Window'是否有一個名字,你的命令是怎樣的?而且因爲我是史努比:你爲什麼要用這個窗口作爲參數?你想做什麼? –

回答

1

其實這對我有用 - 但我使用[MVVM Light Toolkit 1RelayCommand<FrameworkElement>

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

在我的情況下Command來自一個DependencyProperty,但不應該有很大的不同。

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

如此,因爲這是工作 - 我認爲它的Command不支持CommandParameter可能。

+0

編輯我的Q包括命令,將嘗試您的實現,我沒有使用MVVM工具包。 –

+0

也許https://github.com/paulcbetts/mvvmlight/blob/dce4e748c538ed4e5f5a0ebbfee0f54856f52dc6/V3/GalaSoft.MvvmLight/GalaSoft.MvvmLight%20(NET35)/Command/RelayCommandGeneric.cs給你一個提示。 – Peter

+0

這很有趣,直接在Button上執行我的命令,例如,拿起元素綁定爲CommandParameter .eg下面的工作,Inputbinding不是

0

我的建議是使用你的CommandParameter一個RelativeSource結合:

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

這樣你的綁定可以是獨立的,從你的Window的名字。 然後你就可以關閉應用程序的每個窗口中創建一個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(); 
     } 
    } 
} 

我希望它可以幫助你。