c# - WPF RightClick MouseBinding 发布?

标签 c# wpf xaml binding

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

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

最佳答案

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

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

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

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

    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

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

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

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

关于c# - WPF RightClick MouseBinding 发布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3862963/

相关文章:

c# - .Net 5.0 定义的预处理器符号是什么?

.net - 从 ViewModel 中的 Observable 集合中移除不会更新 View,但 Updates of Existing Items 会更新 View

WPF 滚动统一网格

xaml - 在 XAML 中设置 TextBlock 的最大字符长度

c# - 命名空间结合 TFS/Source Control 解释

c# - 使用 IEnumerable GetGenericArguments 获取类型

c# - 在哪里可以找到 ASP.NET Core MVC 中的模型绑定(bind)错误?

c# - Entity Framework 中的僵尸对象

c# - 了解 MVVM - 如何绑定(bind)数据 View ↔ ViewModel + catch 在 View 上按下键并在 viewModel 中启动函数

wpf - 如何在我的 ViewModel 中链接(依赖)属性?