21

WPFアプリに、コマ​​ンドバインディングを含む検索ボタンを含む検索フィールドがあります。これはうまく機能しますが、キーボードのEnterキーを押したときに、テキストフィールドに同じコマンドバインディングを使用するにはどうすればよいですか?私が見た例はすべて、KeyDownイベントハンドラーでコードビハインドを使用しています。これをxamlとコマンドバインディングでのみ機能させるスマートな方法はありますか?

4

5 に答える 5

25

ボタンのIsDefaultプロパティを使用できます。

    <Button Command="SearchCommand" IsDefault="{Binding ElementName=SearchTextBox,
                                               Path=IsKeyboardFocused}">
         Search!
   </Button>
于 2009-10-19T09:07:01.870 に答える
24

受け入れられた回答は、コマンドにバインドされたボタンがすでにある場合にのみ機能します。

この制限を回避するには、TextBox.InputBindingsを使用します。

<TextBox.InputBindings>
    <KeyBinding Key="Enter" Command="{Binding Path=MyCommand}"></KeyBinding>
</TextBox.InputBindings>
于 2012-01-26T03:30:06.150 に答える
3

Prismリファレンス実装には、まさにあなたが求めているものの実装が含まれています。

基本的な手順は次のとおりです。

  • 静的クラスEnterKeyを作成します
  • EnterKeyにICommandタイプの添付プロパティ「Command」を登録しました
  • EnterKeyにEnterKeyCommandBehaviorタイプの添付プロパティ「EnterKeyCommandBehavior」を登録しました
  • 「Command」の値が変更されたら、「EnterKeyCommandBehavior」をEnterKeyCommandBehaviorの新しいインスタンスとしてコントロールにアタッチし、ICommandをビヘイビアーのCommandプロパティに割り当てます。
    • ビヘイビアがすでにアタッチされている場合は、既存のインスタンスを使用します
  • EnterKeyCommandBehaviorは、コンストラクターでUIElementを受け入れ、PreviewKeyDown(またはSilverlightとの互換性を維持したい場合はKeyDown)にアタッチします。
  • イベントハンドラーで、キーがEnterの場合、ICommandを実行します(CanExecuteがtrueの場合)。

これにより、次のような動作を使用できます。

<TextBox prefix:EnterKey.Command="{Binding Path=SearchCommand}" />
于 2009-10-19T08:59:01.263 に答える
0

Greg SamsonのTextBox.Inputsソリューションを試しましたが、依存関係プロパティを介してのみtextinputsにバインドできるというエラーが発生しました。結局、私はこれに対する次の解決策を見つけました。

次のようなCommandReferenceというクラスを作成します。

public class CommandReference : Freezable, ICommand
{
    public CommandReference()
    {
        //
    }

    public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandReference), new PropertyMetadata(new PropertyChangedCallback(OnCommandChanged)));

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        if (Command != null)
            return Command.CanExecute(parameter);
        return false;
    }

    public void Execute(object parameter)
    {
        Command.Execute(parameter);
    }

    public event EventHandler CanExecuteChanged;

    private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        CommandReference commandReference = d as CommandReference;
        ICommand oldCommand = e.OldValue as ICommand;
        ICommand newCommand = e.NewValue as ICommand;

        if (oldCommand != null)
        {
            oldCommand.CanExecuteChanged -= commandReference.CanExecuteChanged;
        }
        if (newCommand != null)
        {
            newCommand.CanExecuteChanged += commandReference.CanExecuteChanged;
        }
    }

    #endregion

    #region Freezable

    protected override Freezable CreateInstanceCore()
    {
        throw new NotImplementedException();
    }

    #endregion
}

Xamlで、これをUserControlリソースに追加します。

<UserControl.Resources>
    <Base:CommandReference x:Key="SearchCommandRef" Command="{Binding Path = SomeCommand}"/>

実際のTextBoxは次のようになります。

 <TextBox Text="{Binding Path=SomeText}">
                    <TextBox.InputBindings>
                        <KeyBinding Command="{StaticResource SearchCommandRef}" Key="Enter"/>
                    </TextBox.InputBindings>
                </TextBox>

このコードをどこから入手したかは覚えていませんが、このサイトでも説明されています。

http://www.netframeworkdev.com/windows-presentation-foundation-wpf/invoke-a-command-with-enter-key-after-typing-in-a-textbox-21909.shtml

于 2013-03-08T08:39:02.093 に答える