2009-06-12 11 views
3

WPFコマンドのルーティングを拡張して、フォーカスされたフィールドでコマンドを呼び出すことができるかどうかを確認したり、そのためのフックはありますか?たぶんあなたはそれがうまくいくかどうか分からないかもしれませんが、ネット上のどこかで似たようなものを見て、リンクを救うことができますか?例えばWPFルーティングコマンドの書き換え方法

抽象例

私はサイドパネルとパネルで、テキストエディタを書くことになる場合、フォーカスを持っているでしょう。 Ctrl + Gを押すと、パネルにコマンドバインディングとフォーカス(通常のWPFの動作)があるため、いくつかのコマンドが呼び出されます。また、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"); 
     } 
    } 
} 

答えて

0

をCtrlキー+ Hを押して実行したいと思いますWindow.AddHandlerメソッドを使用してすべてのルーティングされたコマンドイベントを捕捉してから、それをtextBlock1から再起動します。

textBlock1.RaiseEvent(e);

私はまだそのためのコードを持っていないが、アイデアは、我々は、すべての未処理のイベントをキャッチし、メインウィンドウ領域

からそれらを再提起どこに扱われていない場合ルーティングイベントが、ウィンドウの範囲まで泡立てることです