2011-02-09 5 views
1

WPF의 새로운 기능은이 WPF Routed Command with Bindings per-Tab을 읽고 있었으며 작동에 가깝습니다.WPF ComamndBinding Help on MenuItem

내 RuleTab (tabitem)이 선택 될 때까지 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; 
    } 

!

알아 냈어! 응답 한 사람들에게 감사드립니다. 내가해야 할 일은 명령 바인딩을 이동하는 것입니다 :

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

그런 다음 Command = Find in my MenuItem.

+0

제안? 질문이 뭐야? – DHN

+0

제 질문은 편집 -> 찾기를 클릭하면 내 FindDialog가 아닌 System.Windows.Input.CommandBinding이 표시되는 이유는 무엇입니까? 내 초기 질문 수정 :) –

답변

1

(링크 된 샘플에 따라) TabItem에 CommandBinding을 추가해야합니다. 그런 다음 MenuItem을 바인딩하려면 CommandParameterCommandTarget과 함께 Command 속성을 사용해야합니다 (예상 한 TabItem을 다시 가리킴).

예를 들어, 나는의 ContextMenu에서 MenuItem을이 있고 나는 명령의 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}}}" 
/>