2011-02-09 66 views
1

新的WPF ......正在读这WPF Routed Command with Bindings per-Tab和很接近得到它的工作。WPF ComamndBinding帮助上菜单项

直到我RuleTab(TabItem的)被选择,但不是我的弹出查找对话框它显示在菜单上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 =在我的MenuItem中查找。

+0

建议是什么?问题是什么? – DHN 2011-02-09 20:38:22

回答

1

你会发现你需要添加到的CommandBinding的TabItem的(根据链接的样品)。然后绑定你的菜单项,你应该使用Command属性,可能连同CommandParameterCommandTarget(指回在我预计的TabItem)。

例如,我有一个菜单项中的一个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}}}" 
/>