首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF RightClick MouseBinding on release?

WPF RightClick MouseBinding on release?
EN

Stack Overflow用户
提问于 2010-10-05 18:53:41
回答 1查看 9.6K关注 0票数 4

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

代码语言:javascript
复制
<MouseBinding Command="Close" MouseAction="RightClick" />
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-02-19 12:37:59

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

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

下面是一些示例标记:

代码语言:javascript
复制
<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>

你的旧方法被注释掉了,新方法在它下面。

以下是仅用于连接路由命令的代码隐藏:

代码语言:javascript
复制
    private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        Close();
    }

最后,这里是ExecuteCommand的实现

代码语言:javascript
复制
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

此示例使用行为。如果您不熟悉行为,请安装Expression Blend 4 SDK并添加以下名称空间:

代码语言:javascript
复制
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

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

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3862963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档