1

WPF の新機能...タブごとのバインドを使用してこの WPF Routed Command を読んでいて、それを機能させることに近づいています。

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>

次に、MenuItem で Command=Find を参照します。

4

1 に答える 1

1

CommandBindingをTabItemに追加する必要があることがわかります(リンクされたサンプルのとおり)。次に、MenuItemをバインドするには、Commandプロパティを使用する必要があります。おそらく、CommandParameterand CommandTarget(私が期待する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}}}"
/>
于 2011-02-09T20:43:54.847 に答える