2017-07-14 89 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(); 
     } 
    } 
} 

我希望它可以帮助你。