发布时 WPF 右键单击​​鼠标绑定? [英] WPF RightClick MouseBinding on release?

查看:15
本文介绍了发布时 WPF 右键单击​​鼠标绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何启用鼠标绑定到释放右键?目前我在 xaml 中有以下代码,它链接到关闭 wpf 窗口.这里的问题是,因为它会在关闭窗口时对点击的增加做出反应,所以它会激活桌面上的上下文菜单.

How can I enable a mouse binding to the release of the right button? At the moment I have the following code in xaml which is linked to closing the wpf window. The problem here is that because it reacts to the rampup of the click when closing the window it activates a context menu on the desktop.

<MouseBinding Command="Close" MouseAction="RightClick" />

推荐答案

MouseBinding 不支持鼠标向上操作,只支持鼠标向下操作,因此您根本无法使用鼠标绑定.最简单的替代方法是在您将 MouseBinding 作为 InputBinding 添加到的同一元素上的 MouseRightButtonUp 事件的代码隐藏事件处理程序.但我怀疑您出于自己的原因而避免使用事件处理程序方法,但您应该澄清这是否是您的意图.

The MouseBinding does not support mouse up actions, only mouse down actions, so you simply cannot do what you want to do using a MouseBinding. The simplest alternative is a code-behind event handler for the MouseRightButtonUp event on the same element you would have added the MouseBinding as an InputBinding to. But I suspect you are avoiding the event handler approach for your own reasons, but you should clarify if that is your intention.

剩余的可用选项是某种形式的附加行为.有很多方法可以做到这一点,但我将使用 Blend 行为中相当标准的 System.Windows.Interactivity.您所要做的就是为鼠标右键单击附加一个事件触发器并调用关闭命令.您需要执行此操作的所有内容都在 SDK 中,但不幸的是,调用名为 InvokeCommandAction 的命令的功能无法正确支持路由命令,因此我编写了一个名为 ExecuteCommand 的替代方法.

The remaining option available to use is some form of attached behavior. There are many ways to do this but I'll use the fairly standard System.Windows.Interactivity from Blend behaviors. All you have to do is attach an event trigger for right mouse button up and invoke the close command. Everything you need to do this is in the SDK but unfortunately the feature to invoke a command called InvokeCommandAction doesn't properly support routed commands so I've written an alternative called ExecuteCommand.

这是一些示例标记:

<Grid Background="White">
    <Grid.CommandBindings>
        <CommandBinding Command="Close" Executed="CommandBinding_Executed"/>
    </Grid.CommandBindings>
    <!--<Grid.InputBindings>
        <MouseBinding Command="Close" MouseAction="RightClick"/>
    </Grid.InputBindings>-->
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseRightButtonUp">
            <utils:ExecuteCommand Command="Close"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <StackPanel>
        <TextBox Text="Some Text"/>
    </StackPanel>
</Grid>

您的旧方法已被注释掉,新方法在其下方.

Your old method is commented out and the new method is below it.

这是用于连接路由命令的代码隐藏:

Here is the code-behind just to hook up the routed command:

    private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        Close();
    }

最后,这里是ExecuteCommand的实现:

public class ExecuteCommand : TriggerAction<DependencyObject>
{
    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), typeof(ExecuteCommand), null);

    public object CommandParameter
    {
        get { return (object)GetValue(CommandParameterProperty); }
        set { SetValue(CommandParameterProperty, value); }
    }

    public static readonly DependencyProperty CommandParameterProperty =
        DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExecuteCommand), null);

    public UIElement CommandTarget
    {
        get { return (UIElement)GetValue(CommandTargetProperty); }
        set { SetValue(CommandTargetProperty, value); }
    }

    public static readonly DependencyProperty CommandTargetProperty =
        DependencyProperty.Register("CommandTarget", typeof(UIElement), typeof(ExecuteCommand), null);

    protected override void Invoke(object parameter)
    {
        if (Command is RoutedCommand)
        {
            var routedCommand = Command as RoutedCommand;
            var commandTarget = CommandTarget ?? AssociatedObject as UIElement;
            if (routedCommand.CanExecute(CommandParameter, commandTarget))
                routedCommand.Execute(CommandParameter, commandTarget);
        }
        else
        {
            if (Command.CanExecute(CommandParameter))
                Command.Execute(CommandParameter);
        }
    }
}

如果您不使用路由命令而是使用 MVVM RelayCommand,则您可以不需要 ExecuteCommand,而可以使用 InvokeCommandAction 代替.

If you are not using routed commands but are using say, an MVVM RelayCommand, you can don't need ExecuteCommand and you can use InvokeCommandAction instead.

这个例子使用了行为.如果您不熟悉行为,请安装 Expression Blend 4 SDK 并添加此命名空间:

This example uses behaviors. If you are not familiar with behaviors, install the Expression Blend 4 SDK and add this namespace:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

并将 System.Windows.Interactivity 添加到您的项目中.

and add System.Windows.Interactivity to your project.

这篇关于发布时 WPF 右键单击​​鼠标绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆