按下WPF按键,修改XAML / MVVM中的点击命令

在WPF中,我可以轻松地在ViewModel中创建命令和命令处理程序,并通过遵循标准MVVM设计模式轻松将其连接到XAML(View)中的Button控件。 我也可以在XAML(View)中定义InputBindings和CommandBindings来处理关键帧,然后在ViewModel中执行命令。 目前在按钮上有一个命令,当点击按钮时它会被执行。 但是,我怎样才能在同一时间点击按钮,如果按下了键修饰符,然后执行另一个命令? 键修饰符是左或右Alt。


你可以实现一个附加的行为:

namespace WpfApplication1
{
    public class CombinedMouseAndKeyCommandBehavior
    {
        public static readonly DependencyProperty KeyProperty = DependencyProperty.RegisterAttached("Key", typeof(Key),
           typeof(CombinedMouseAndKeyCommandBehavior), new PropertyMetadata(Key.None, new PropertyChangedCallback(OnKeySet)));

        public static Key GetKey(FrameworkElement element) => (Key)element.GetValue(KeyProperty);

        public static void SetKey(FrameworkElement element, Key value) => element.SetValue(KeyProperty, value);

        public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand),
            typeof(CombinedMouseAndKeyCommandBehavior), new PropertyMetadata(null));

        public static ICommand GetCommand(FrameworkElement element) => (ICommand)element.GetValue(CommandProperty);

        public static void SetCommand(FrameworkElement element, ICommand value) => element.SetValue(CommandProperty, value);

        private static void OnKeySet(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement fe = d as FrameworkElement;
            fe.PreviewMouseLeftButtonDown += Fe_PreviewMouseLeftButtonDown;
        }

        private static void Fe_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement fe = sender as FrameworkElement;
            Key key = GetKey(fe);
            ICommand command = GetCommand(fe);
            if(key != Key.None && command != null && Keyboard.IsKeyDown(key))
            {
                command.Execute(null);
            }
        }
    }
}

用法:

<Button Content="Test command"
                xmlns:local="clr-namespace:WpfApplication1"
                local:CombinedMouseAndKeyCommandBehavior.Command="{Binding RemoveCommand}"
                local:CombinedMouseAndKeyCommandBehavior.Key="F">
    <Button.InputBindings>
        <MouseBinding Gesture="LeftClick" Command="{Binding AddCommand }" />
    </Button.InputBindings>
</Button>

WPF中附加行为简介: https //www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPF


您可以只阅读专注的WPF元素来阅读按下的键。 在你的情况下,你可以从窗口(页面)获取它。

XAML

<Window x:Class="Application.MainWindow"
    mc:Ignorable="d"
    Title="MainWindow"
    KeyDown="MainWindow_OnKeyDown"
    x:Name="RootKey">

代码隐藏

private void MainWindow_OnKeyDown(object sender, KeyEventArgs e)
{
    var dataContext = (MainPageViewModel) this.DataContext;
    dataContext.KeyModifer = e.SystemKey.ToString();
}

视图模型

internal class MainPageViewModel : ViewModelBase
{
    public string KeyModifer { private get; set;}
    ...
}

通过在下面的输入绑定示例上设置修饰符属性,在XAML中执行此操作。

<TextBox.InputBindings>
    <KeyBinding Key="Enter" Command="{Binding SaveCommand}" Modifiers="Alt"/>
    <KeyBinding Key="Enter" Command="{Binding AnotherSaveCommand}"/>
</TextBox.InputBindings>
链接地址: http://www.djcxy.com/p/56141.html

上一篇: WPF Button key down modifies click command in XAML/MVVM

下一篇: Close window MVVM style