如何重写WPF路由命令的调度机制 [英] How to rewrite WPF routed commands dispatching mechanism

查看:95
本文介绍了如何重写WPF路由命令的调度机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能否以某种方式扩展WPF命令的路由,以便它首先检查命令是否可以在焦点字段中调用,如果不能在其他字段中调用(从不更改)?是否有任何挂钩?也许您不知道这样是否可行,但在网上看到类似的内容并可以保留链接?

Can I extend WPF commands routing in a way so it would first check whether command can be invoked in the focused field and if not in some other (never changing)? Is there any hooks for that? Maybe you don't know whether that would work but saw something similar somewhere on the net and can spare the link?

抽象示例

例如,如果我要编写一个带有侧面板的文本编辑器,并且面板将具有焦点.如果我按Ctrl + G,将调用某些命令,因为面板具有命令绑定和焦点(正常的WPF行为).另外,如果我按Ctrl + H,但是此时间面板没有被调用命令的命令绑定.在这种情况下,我希望路由引擎切换到文本编辑器,然后将相同的命令冒泡到那里.

For example if I would be writing a text editor with a side panel and panel would have the focus. If I would press Ctrl+G some command will be invoked because panel has command binding and focus (that normal WPF behavior). Also if I press Ctrl+H but this time panel have no command binding for invoked command. In this case I would want routing engine to switch to text editor and bubble same command there to.

真实示例

我有一个菜单项,上面写着粘贴",但焦点在侧面板上.如果按菜单键,将执行绑定到面板的命令.假设没有适当的命令绑定到面板.在这种情况下,我想粘贴到文本编辑器中.

I have a menu item which says Paste but focus is on the side panel. If I press menu command binded to panel will be executed. Suppose there is no appropriate command binded to panel. In this case I would like to paste into text editor.

代码

此代码或多或少代表了这种情况.我想按Ctrl + H并执行CommandBinding_Executed_1

This code more or less represents this scenario. I would like to press Ctrl+H and execute CommandBinding_Executed_1

Window1.xaml

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1">
    <StackPanel>        
        <TextBox x:Name="textBlock1">
            <TextBox.CommandBindings>
                <CommandBinding Command="local:Window1.TestCommand" Executed="CommandBinding_Executed_1" />
                <CommandBinding Command="local:Window1.ForwardedTestCommand" Executed="CommandBinding_Executed_1" />
            </TextBox.CommandBindings>
        </TextBox>
        <TextBox x:Name="textBlock2">
            <TextBox.CommandBindings>
                <CommandBinding Command="local:Window1.TestCommand" Executed="CommandBinding_Executed_2" />
            </TextBox.CommandBindings>
            <TextBox.InputBindings>
                <KeyBinding Command="local:Window1.TestCommand" Gesture="Ctrl+G" />
                <KeyBinding Command="local:Window1.ForwardedTestCommand" Gesture="Ctrl+H" />
            </TextBox.InputBindings>
        </TextBox>
    </StackPanel>
</Window>

Window1.xaml.cs

using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public static RoutedUICommand TestCommand = new RoutedUICommand("TestCommand", "TestCommand", typeof(Window1));
        public static RoutedUICommand ForwardedTestCommand = new RoutedUICommand("ForwardedTestCommand", "ForwardedTestCommand", typeof(Window1));

        public Window1()
        {
            InitializeComponent();
        }

        private void CommandBinding_Executed_1(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("CommandBinding_Executed_1");
        }

        private void CommandBinding_Executed_2(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("CommandBinding_Executed_2");
        }
    }
}

推荐答案

我能够解决此问题的方法是使用Window.AddHandler方法捕获所有路由的命令事件,然后像这样从textBlock1重新引发它们.

The way I was able to solve this is using Window.AddHandler method to catch all routed command events and then re-raise them from textBlock1 like so.

textBlock1.RaiseEvent(e);

textBlock1.RaiseEvent(e);

我还没有代码,但是想法是将路由事件(如果未处理)冒泡到窗口范围,在该范围内我们捕获所有未处理的事件并从主窗口区域重新引发它们.

I don't have a code for that yet, but the idea is that routed event if not handled is bubbled up to window scope where we catch all unhandled events and re-raise them from main window area

这篇关于如何重写WPF路由命令的调度机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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