输入绑定未触发命令wpf wvvm

时间:2018-11-29 03:37:22

标签: wpf mvvm wpf-controls

我在MainWindow.xaml中有这个

<Window.InputBindings>
        <KeyBinding Modifiers="Ctrl" Key="Delete"  Command="{Binding DelAllMessages}"/>
    </Window.InputBindings>

和在MainViewModel中

public void DelAllMessages()
        {
            MessageBoxResult result = MessageBox.Show(
                "Are you sure you want to delete?",
                                          "Confirmation",
                                          MessageBoxButton.YesNo,
                                          MessageBoxImage.Question);
            if (result == MessageBoxResult.Yes)
            {
                // todo
            }
        }
}

当我在窗口的键盘中按ctrl + del时,此方法不会起作用。缺少什么?

1 个答案:

答案 0 :(得分:1)

您需要使用命令而不是直接绑定方法。要记住的一个想法是,要通过属性来在View模型和View之间进行通信。

步骤1:-

创建一个Command Handler类并实现ICommand,如下面的代码所示。

   public class CommandHandler : ICommand
{
    private Action<object> _action;
    private bool _canExeute;
    public event EventHandler CanExecuteChanged;

    private bool canExeute
    {
        set
        {
            _canExeute = value;
            CanExecuteChanged(this, new EventArgs());
        }
    }


    public CommandHandler(Action<object> action, bool canExecute)
    {
        _action = action;
        _canExeute = canExecute;
    }
    public bool CanExecute(object parameter)
    {
        return _canExeute;
    }

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

步骤2:- 在您的Window后面的代码中使用新创建的Command类。

创建一个ICommand的属性,并提供DelAllMessages()作为对该命令的操作,因此当按下Clt + Del时,它将调用您的方法。

    private ICommand _KeyCommand;
    public ICommand KeyCommand
    {
        get { return _KeyCommand ?? (_KeyCommand = new CommandHandler(obj => DelAllMessages(), true)); }
    }

步骤3:-

将您的窗口类分配为DataContext到窗口的Xaml。

this.DataContext = this;

查看整个课程代码。

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    private ICommand _KeyCommand;
    public ICommand KeyCommand
    {
        get { return _KeyCommand ?? (_KeyCommand = new CommandHandler(obj => DelAllMessages(), true)); }
    }


    public void DelAllMessages()
    {
        MessageBoxResult result = MessageBox.Show(
            "Are you sure you want to delete?",
            "Confirmation",
            MessageBoxButton.YesNo,
            MessageBoxImage.Question);
        if (result == MessageBoxResult.Yes)
        {
            // todo
        }
    }
}

步骤4:-

在Xaml中绑定新创建的Command属性。

<Window.InputBindings>
    <KeyBinding Modifiers="Ctrl" Key="Delete"  Command="{Binding KeyCommand}"/>
</Window.InputBindings>

整个Xaml代码:-

<Window x:Class="WpfApp4.TriggerTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp4"
    mc:Ignorable="d"
    Title="Window1" Height="450" Width="800">
<Window.InputBindings>
    <KeyBinding Modifiers="Ctrl" Key="Delete"  Command="{Binding KeyCommand}"/>
</Window.InputBindings>
<Grid>

</Grid>
</Window>