2011-02-09 6 views
1

WPFの初心者は、このWPF Routed Command with Bindings per-Tabを読んでいました。WPF ComamndBinding Help on MenuItem

メニュータブは、ルールタブ(タブアイテム)が選択されるまで表示されず、メニューのSystem.Windows.Input.CommandBindingを表示する検索ダイアログが表示されます。私は間違って何をしていますか?

XAML:

<MenuItem Header="_Find..." IsEnabled="{Binding ElementName=RuleTab, Path=IsSelected}" > 
        <CommandBinding Command="Find" Executed="ExecuteFind" CanExecute="Find_CanExecute" ></CommandBinding> 
       </MenuItem> 

は、コードビハインド:

 private void ExecuteFind(object sender, ExecutedRoutedEventArgs e) 
    { 
     // Initiate FindDialog 
     FindDialog dlg = new FindDialog(this.RuleText); 

     // Configure the dialog box 
     dlg.Owner = this; 
     dlg.TextFound += new TextFoundEventHandler(dlg_TextFound); 

     // Open the dialog box modally 
     dlg.Show(); 
    } 

    void dlg_TextFound(object sender, EventArgs e) 
    { 
     // Get the find dialog box that raised the event 
     FindDialog dlg = (FindDialog)sender; 

     // Get find results and select found text 
     this.RuleText.Select(dlg.Index, dlg.Length); 
     this.RuleText.Focus(); 
    } 

    private void Find_CanExecute(object sender, CanExecuteRoutedEventArgs e) 
    { 
     e.CanExecute = RuleTab.IsSelected; 
    } 

任意の提案をいただければ幸いです!

それを実感してください!回答した人に感謝します。その後、私のMenuItemで検索=コマンドを参照

<Window.CommandBindings> 
<CommandBinding Command="Find" Executed="ExecuteFind" CanExecute="Find_CanExecute" ></CommandBinding> 
</Window.CommandBindings> 

:私がしなければならなかったすべては私にいるCommandBindingを移動しました。

+0

提案内容に?質問はなんですか? – DHN

+0

私の質問は、編集 - > Find FindDialogを表示するのではなくSystem.Windows.Input.CommandBindingをなぜ表示するのかをクリックすることです。私の最初の質問を編集:) –

答えて

1

CommandBindingを(リンクされたサンプルごとに)TabItemに追加する必要があることがわかります。次に、MenuItemをバインドするにはCommandParameterCommandTarget(おそらく私が期待するTabItemを指す)と共にCommandプロパティを使用する必要があります。

例えば、私はのContextMenuにメニューアイテムを持っていると私はのContextMenuのコンテキスト(配置対象)の火災へのコマンドをしたい:

<MenuItem Header="View" 
      ToolTip="Open the Member Central view for this member" 
      Command="{x:Static local:Commands.CustomerViewed}" 
      CommandParameter="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" 
      CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" 
/>