3

ある方法で WPF コマンド ルーティングを拡張して、コマンドをフォーカスされたフィールドで呼び出すことができるかどうかを最初に確認できますか? そのためのフックはありますか?それが機能するかどうかわからないかもしれませんが、ネット上のどこかで似たようなものを見たので、リンクを惜しみませんか?

抽象的な例

たとえば、サイド パネルを備えたテキスト エディタを作成している場合、パネルにフォーカスがあります。Ctrl+G を押すと、パネルにコマンド バインディングとフォーカスがあるため (通常の WPF 動作)、いくつかのコマンドが呼び出されます。また、Ctrl+H を押しても、今回はパネルに呼び出されたコマンドのコマンド バインドがありません。この場合、ルーティング エンジンをテキスト エディターに切り替えて、同じコマンドをそこに吹き込みたいと思います。

実際の例

[貼り付け] というメニュー項目がありますが、フォーカスはサイド パネルにあります。メニューを押すと、パネルにバインドされたコマンドが実行されます。パネルにバインドされた適切なコマンドがないとします。この場合、テキスト エディターに貼り付けたいと思います。

コード

このコードは、多かれ少なかれこのシナリオを表しています。Ctrl+H を押して 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");
        }
    }
}
4

1 に答える 1